Until now I've been loading a bitmap into my RemoteViews
directly using remoteViews.setImageViewBitmap()
. It's working fine in general.
But a couple of users are having issues, and I think it is when loading in a bitmap that is very large. I cache the bitmap to local storage already anyway, so my idea is to use setImageViewUri()
instead, as recommended elsewhere.
But I can't get it to work... I just get a blank widget. The following snippet illustrates what I'm doing (leaving out some the context but hopefully leaving enough)...
// Bitmap bmp is fetched from elsewhere... and then...
FileOutputStream fos = context.openFileOutput("img.png", Context.MODE_PRIVATE);
bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
// ... later ...
// this works...
remoteViews.setImageViewBitmap(R.id.imageView, bmp);
// but this doesn't...
remoteViews.setImageViewUri(R.id.imageView, Uri.fromFile(new File(context.getFilesDir().getPath(), "img.png")));
EDIT
Trying to use FileProvider
as suggested by CommonsWare below...
Manifest:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.xyz.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
xml/file_paths.xml:
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="cache" path="."/>
</paths>
code:
File imagePath = new File(context.getFilesDir(), ".");
File newFile = new File(imagePath, "img.png"); // img.png created as above, in top level folder
remoteViews.setImageViewUri(R.id.imageView, FileProvider.getUriForFile(context, "com.xyz.fileprovider", newFile));
But still I'm left with a blank widget, as before, and without an error in the logcat.