【发布时间】:2016-05-10 09:35:20
【问题描述】:
到目前为止,我一直使用remoteViews.setImageViewBitmap() 直接将位图加载到我的RemoteViews 中。总体来说运行良好。
但是有几个用户遇到了问题,我认为是在加载非常大的位图时。无论如何,我已经将位图缓存到本地存储,所以我的想法是改用setImageViewUri(),正如其他地方所推荐的那样。
但我无法让它工作......我只是得到一个空白小部件。下面的 sn-p 说明了我在做什么(省略了一些上下文,但希望留下足够的内容)......
// 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")));
编辑
按照以下 CommonsWare 的建议尝试使用 FileProvider...
清单:
<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>
代码:
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));
但我仍然像以前一样留下一个空白小部件,并且在 logcat 中没有错误。
【问题讨论】:
标签: android android-appwidget remoteview android-remoteview