0

I am currently working on passing image as Base64 to a rest service. But I am not able to read the file. Here is my code:

if (requestCode == iPictureCode && resultCode == RESULT_OK) { String picturePath = data.getStringExtra(Intents.EXTRA_PICTURE_FILE_PATH); String image = processPictureWhenReady(picturePath); }


`private String processPictureWhenReady(final String picturePath) { final File pictureFile = new File(picturePath);

  if (pictureFile.exists()) {
        // The picture is ready; process it.
        Bitmap bitmap = BitmapFactory.decodeFile(pictureFile.getAbsolutePath());
        bitmap = CameraUtils.resizeImageToHalf(bitmap);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 85, stream);
        String base64Image = Base64.encodeToString(stream.toByteArray(), Base64.DEFAULT);
        return base64Image;
    }

}`

it never enters the if block for 'pictureFile.exists()'

What could be the problem?

pt2121
  • 11,720
  • 8
  • 52
  • 69
ipradhansk
  • 352
  • 1
  • 10
  • 36

1 Answers1

2

It looks like you adapted your code from the example in the developer docs, but you removed the else clause where a FileObserver is used to detect when the image is actually available. You need that part as well because the full-sized image may not be ready immediately when the activity returns.

Tony Allevato
  • 6,429
  • 1
  • 29
  • 34
  • Thanks @Tony for your response...I removed that piece of code in my question...however its working for me now when i replaced `EXTRA_PICTURE_FILE_PATH` with `EXTRA_THUMBNAIL_FILE_PATH` – ipradhansk Sep 22 '14 at 03:58
  • 1
    The thumbnail image will always be ready immediately when the activity result is returned, so if you only need a low resolution image, you can just use that. You'll still need the `FileObserver` to detect the full-size image if you need it, however. The reason the thumbnail is immediately available is so that you can display something in your UI as confirmation that the picture was taken while the full image is being prepared. – Tony Allevato Sep 22 '14 at 15:27