0

I am trying to convert an image to byte array and converting back byte array to image in Android Emulator. First part is working fine but the second part is not creating the image file in Android emulator.

Please suggest me if there is any correction in my second part of the code.

Following is my code.

public String GetQRCode() throws FileNotFoundException, IOException {
    /*
     * In this function the first part shows how to convert an image file to 
     * byte array. The second part of the code shows how to change byte array
     * back to an image.
     */
    Bitmap bitmap = BitmapFactory.decodeFile("sdcard/Download/QR.jpg");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 60, baos); 
    byte[] byte_img_data = baos.toByteArray();  

    byte[] buf = new byte[200];

  // Second Part: Convert byte array back to an image

    Bitmap bitmap2 = BitmapFactory.decodeByteArray(byte_img_data, 0, 200);

    ByteArrayOutputStream img= new ByteArrayOutputStream();

    Bitmap imageFile= BitmapFactory.decodeFile("sdcard/Download/QR3.jpg");

    String abc = buf.toString();
    return abc; 
}
2
  • Is there any reason to believe this would work (or fail) differently in the emulator than in a phone or tablet? If not, this has nothing to do with the emulator. Commented Mar 22, 2012 at 13:42
  • can you try @Chris White answer? Commented Apr 18, 2013 at 7:20

1 Answer 1

1

your call to BitmapFactory.decodeByteArray(..) - this method returns a Bitmap object, but you're not assigning it to anything. You also need to change the call to pass in the actual length of byte_img_data, rather then 200.

Bitmap bitmap2 = BitmapFactory.decodeByteArray(byte_img_data, 0, byte_img_data.length);

However, whether decodeByteArray(..) can handle compressed streams, i don't know

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.