I do some processing (quality improvement and a bit of resizing) on a Bitmap object and then store it using bitmap.compress() function by giving a file name “myfile.png”.
newbitmap = processImage(bitmap);
FileOutputStream fos = context.openFileOutput("myfile.png", Context.MODE_PRIVATE);
newbitmap.compress(CompressFormat.PNG, 100, fos);
Now I want to load this image in an ImageView but I cant use setImageBitmap() to do that. Is there any alternative ?
The reason I cant use setImageBitmap() is that I am using RemoteViews for a widget, and using bitmap method leads to Failed Binder Transaction error when the image is large.
I tried to set image uri using the code below but the image does not load on the ImageView:
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
File internalFile = context.getFileStreamPath("myfile.png");
Uri internal = Uri.fromFile(internalFile);
rv.setImageViewUri(R.id.widgetImageView, internal);
updateAppWidget(awID, rv);
Thanks for your help!
While storing images in internal storage, you have to set the mode to
MODE_WORLD_READABLEinstead ofMODE_PRIVATE. This is because a widget is handled by the home screen process and it cant access your files if you dont set the mode right.So replace this line:
with:
Also, use
Uri.parse()andgetPath()instead offromFile().fromFile()works only on Android 2.2 and above.Hope this helps!