3

我正在尝试在 Android 4.2 锁屏上的应用程序中启用我的小部件,它工作正常,但是它无法解析任何图像 uri。我得到的错误是这样的:

java.io.FileNotFoundException: /storage/emulated/0/Android/data/com.tvshowfavs/cache/22kifo7sdmyet7x7kphdgch69: open failed: EACCES (Permission denied)
    at libcore.io.IoBridge.open(IoBridge.java:416)
    at java.io.FileInputStream.<init>(FileInputStream.java:78)
    at java.io.FileInputStream.<init>(FileInputStream.java:105)
    at android.content.ContentResolver.openInputStream(ContentResolver.java:447)
    at android.widget.ImageView.resolveUri(ImageView.java:636)
    at android.widget.ImageView.setImageURI(ImageView.java:381)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at android.widget.RemoteViews$ReflectionAction.apply(RemoteViews.java:1146)
    at android.widget.RemoteViews.performApply(RemoteViews.java:2304)
    at android.widget.RemoteViews.apply(RemoteViews.java:2263)
    at android.widget.RemoteViewsAdapter$RemoteViewsFrameLayout.onRemoteViewsLoaded(RemoteViewsAdapter.java:286)
    at android.widget.RemoteViewsAdapter$RemoteViewsFrameLayoutRefSet.notifyOnRemoteViewsLoaded(RemoteViewsAdapter.java:335)
    at android.widget.RemoteViewsAdapter$4.run(RemoteViewsAdapter.java:993)
    at android.os.Handler.handleCallback(Handler.java:725)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.os.HandlerThread.run(HandlerThread.java:60)
    Caused by: libcore.io.ErrnoException: open failed: EACCES (Permission denied)
    at libcore.io.Posix.open(Native Method)
    at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
    at libcore.io.IoBridge.open(IoBridge.java:400)

任何人都知道为什么它无法打开存储在模拟存储中的图像?此图像之前已使用 Environment.getExternalStorageDirectory() 下载并缓存,因此应正确写入存储目录。这是在尝试加载任何图像时收到此异常的 Nexus 10 上。任何帮助表示赞赏。

4

2 回答 2

0

Alright, I figured out the problem. I was using the RemoteView method setImageUri(), which it looks as though won't work since the different process can't access the file. The solution is to use setImageViewBitmap() and to decode the bitmap yourself, like this, for example: BitmapFactory.decodeFile(Uri.parse(uri).getPath())

I find it strange that the setImageUri() works fine with launchers but not on the lockscreen, however.

于 2012-12-01T19:39:48.500 回答
0

托马斯是对的。使用content:Uri 而不是file:Uri 为我解决了这个问题。

请参阅此示例项目,演示如何通过此 SO 答案中的 ContentProvider提供文件

于 2013-06-05T23:12:08.943 回答