0

Any ideas how I can extract an image from a JSON object and pass to another Activity via intent to another object? So far this is as closest I've got is recieving a byte array in the recieving activity, but BitmapFactory.decodeByteArray returns 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);

In the receiving fragment:

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 returns null

Update

I messed up, the JSON JsonNode attachmentText = item.get("_attachments"); wasn't returning an image but the image meta data instead.

I've tried going a different route and converting an ImageView from the view layout to a byte array and passing that to the activity instead, but that doesn't work either, bmp returns 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);

Is the only thing left to do query the database again for the images I've already retrieved? Currently they are in a listView, and im trying to get the selected list items image to show in the detail view attached to the new activity.

Here's the working code which originally retrieves the images for the listview:

 AttachmentInputStream readAttachment = db.getAttachment(row.getId(), imgId);
 Bitmap bitmap = BitmapFactory.decodeStream(readAttachment);
 img.setImageBitmap(bitmap);
KingFu
  • 1,257
  • 5
  • 20
  • 40

1 Answers1

0

Once you know you have the image JSON string ...

private Bitmap getBitmapFromString(String jsonString) {
    byte[] decodedString = Base64.decode(stringPicture, Base64.DEFAULT);
    Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
    return decodedByte;
}
Bill Mote
  • 12,067
  • 7
  • 49
  • 77