-4

I am trying to read all the photos which I put to the sdcard via Android Device Monitor, I can get the path such as /storage/emulated/0/lemon.jpg

Could you please tell me how I could convert this photo into bitmap?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Ziiiijun
  • 3
  • 2

1 Answers1

0

If you are targeting Android 4.4 (API level 19) and above you can use Storage Access Framework. Open a document using SAF, get Uri on onActivityResult() and use Uri to open image and turn it into a Bitmap.

Once you have the URI for a document, you can open it or do whatever else you want to do with it.

private Bitmap getBitmapFromUri(Uri uri) throws IOException {
    ParcelFileDescriptor parcelFileDescriptor =
            getContentResolver().openFileDescriptor(uri, "r");
    FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
    Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
    parcelFileDescriptor.close();
    return image;
}
Thracian
  • 43,021
  • 16
  • 133
  • 222
  • I try this method, but when I set the imageView, i get this error, Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference – Ziiiijun Oct 21 '17 at 14:07