JSONオブジェクトから画像を抽出し、別のオブジェクトへのインテントを介して別のアクティビティに渡す方法はありますか? これまでのところ、これは受信アクティビティでバイト配列を受信することに最も近いものですが、BitmapFactory.decodeByteArray は null を返します。
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
DetailFragment fragB = (DetailFragment) getSupportFragmentManager()
.findFragmentById(R.id.detail_frag);
Row row = (Row)parent.getItemAtPosition(position);
JsonNode item = row.getValueAsNode();
intent.putExtra("item", item.toString() );
JsonNode attachmentText = item.get("_attachments");
//hidden code which gets the imgId from the JsonNode
byte[] byteArray = attachmentText.get(imgId).toString().getBytes();
intent.putExtra("picture", byteArray);
startActivity(intent);
受信フラグメント:
byte[] byteArray = getArguments().getByteArray("picture");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView image = (ImageView) view.findViewById(R.id.imgDetail);
image.setImageBitmap(bmp);
bmp は null を返します
アップデート
私は失敗しました.JSONJsonNode attachmentText = item.get("_attachments");
は画像ではなく画像のメタデータを返しました.
別のルートに進み、ImageView をビュー レイアウトからバイト配列に変換し、代わりにそれをアクティビティに渡そうとしましたが、どちらも機能しません。bmp は null を返します。
ImageView image = (ImageView) view.findViewById(R.id.img);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), image.getId());
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
//pass Byte Array to intent
intent.putExtra("picture", byteArray);
既に取得した画像について、データベースに再度クエリを実行する必要がありますか? 現在、それらはlistViewにあり、選択したリスト項目の画像を取得して、新しいアクティビティに添付された詳細ビューに表示しようとしています。
リストビューの画像を最初に取得する作業コードは次のとおりです。
AttachmentInputStream readAttachment = db.getAttachment(row.getId(), imgId);
Bitmap bitmap = BitmapFactory.decodeStream(readAttachment);
img.setImageBitmap(bitmap);