0

I am opening the gallery and getting the path of an image when it is selected. Then I create a Bitmap from this path and store this bitmap in internal storage through bitmap.compress() function. Is it possible to display this image in ImageView by specifying the path of this stored bitmap in setImageUri() function of ImageView ?

Bitmap bmap = BitmapFactory.decodeFile(selectedImagePath);
FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
bmap.compress(CompressFormat.PNG, 100, fos);
fos.close();

Here, fileName is an integer (actually an ID for a widget) converted into String.

Would it be possible to load this image in ImageView like this:

File internalFile = getFileStreamPath(fileName);
Uri internal = Uri.fromFile(internalFile);
imageView.setImageUri(internal);

I know I can set the image using setImageBitmap() function but that would require me to read the Bitmap from the file and pass it through the Parcel object which leads to Failed Binder Transaction error when the images are large. I am making a widget which displays an image through ImageView.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Shubham
  • 780
  • 3
  • 13
  • 32

1 Answers1

1

The setImageUri() method is not intended to be used that way, see this question and answers. The best way seems to be scaling the bitmap to the view before calling setImageBitmap(), e.g. by using Bitmap.createBitmap() with a Matrix parameter

Community
  • 1
  • 1
comodoro
  • 1,489
  • 1
  • 19
  • 30
  • I cant use setImageBitmap(). Before storing this bitmap, I am already doing some processing on it so as to improve the quality and a bit of resizing. If I do further scaling, it may not give the expected result. – Shubham Aug 11 '12 at 19:02
  • If I could find a way to store this processed bitmap in internal storage and later provide its uri to the ImageView, it will solve my problem. – Shubham Aug 11 '12 at 19:04
  • I do not understand: when displayed in ImageView, the image is scaled (or cropped depending on the settings) on display anyway, I see no significant difference to doing it manually (except perhaps some slight speed optimization in the ImageView). On the contrary, if you scale it (or do whatever you want with it) yourself, you have absolute control over whatever gets displayed. – comodoro Aug 12 '12 at 12:35
  • Alright I will try what you suggested. But I dont understand why setImageUri() is not working. A lot of people have posted that if we use Uri.parse() then it works. But in my case, this too gives a blank ImageView. Check here: http://code.google.com/p/android/issues/detail?id=2733 – Shubham Aug 12 '12 at 13:06