'Display image stored in internal storage in an ImageView ? What am I doing wrong?
I first store an image in internal storage this way:
FileOutputStream fos = context.openFileOutput("myimage.png", Context.MODE_PRIVATE);
bitmap.compress(CompressFormat.PNG, 100, fos);
fos.close();
And then I display it in an ImageView this way:
File filepath = context.getFileStreamPath("myimage.png");
Uri uri = Uri.parse(filepath.toString());
myImageView.setImageUri(uri);
But the image never appears in the ImageView. I cannot use setImageBitmap() here due to some constraints. Can anyone tell me where am I going wrong ?
Thanks for your help!
Solution 1:[1]
You can try the following:
Instead of context.getFileStreamPath("myimage.png"); you can try Uri uri = Uri.parse(getFilesDir().getPath() + "/myimage.png");
Solution 2:[2]
BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), getFileStreamPath("myimage.png").getAbsolutePath());
imageView.setImageDrawable(bitmapDrawable);
or
Bitmap b = BitmapFactory.decodeFile(getFileStreamPath("myimage.png").getAbsolutePath());
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | FredFloete |
| Solution 2 | Sojurn |
