Dongfang Fu
Dongfang Fu

Reputation: 9

In android, BitmapFactory.decodeFile do not work

I use the following code to get a image in sd card, and then show the image by ImageView:

Bitmap bmp = BitmapFactory.decodeFile(imgUri.getPath());
img.setImageBitmap(bmp);

However, nothing is showed. I had edit the AndroidManifest.xml in this way:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

But that does not work. Now I know that the image in sd card did not be read by the program. How can I solve the problem? Ps: The phone I use is RedMi 4X, the operating system is MIUI8.

Upvotes: 0

Views: 802

Answers (1)

Lawliet
Lawliet

Reputation: 3499

Because file:// or content:// makes it incorrect. Try this:

Uri uri;
Context context;
try {
    Bitmap bmp = BitmapFactory.decodeStream(context.getContentResolver().openInputStream(imgUri));
img.setImageBitmap(bmp);
} catch (FileNotFoundException e) {
    e.printStackTrace();
}

Upvotes: 1

Related Questions