【问题标题】:resolveUri failing on app private data fileresolveUri 在应用程序私有数据文件上失败
【发布时间】:2013-01-04 05:28:07
【问题描述】:

当我的应用程序首次运行时,我会生成一堆图像文件,然后尝试使用 remoteViews.setImageViewUri() 将这些图像放入我的 ImageViews 中。但是,当我运行我看到的应用程序时

01-03 15:05:11.252:W/ImageView(137):无法打开内容: file:///data/data/com.nickavv.cleanwidgets/files/batt_s_19.png 01-03 15:05:11.252:W/ImageView(137):java.io.FileNotFoundException: /data/data/com.nickavv.cleanwidgets/files/batt_s_19.png(权限 拒绝)

既然文件是由这个应用程序创建的,我应该有权从这个应用程序中访问它,对吧?这是我用来检索它的代码:

   File file = new File(context.getFilesDir().getPath(), idName+".png");
   Uri newUri = Uri.fromFile(file);
   myViews.setImageViewUri(id, newUri);

有什么想法吗?

【问题讨论】:

    标签: android file-io android-appwidget remoteview


    【解决方案1】:

    不要使用Uri.parse,而是使用Uri.fromfile

    Uri newUri = Uri.fromFile(file);
    

    【讨论】:

    • 这将我的错误更改为:01-03 15:05:11.252: W/ImageView(137): Unable to open content: file:///data/data/com.nickavv.cleanwidgets/files/batt_s_19.png 01-03 15:05:11.252: W/ImageView(137): java.io.FileNotFoundException: /data/data/com.nickavv.cleanwidgets/files/batt_s_19.png (Permission denied) 为了清楚起见已添加到原始帖子中
    • 那是权限问题。 URI 是正确的,但不允许您访问该文件。您的 RemoteView 是否与生成图像的进程不同?
    • 图像是在小部件的 OnEnabled 方法中生成的,我试图在 OnUpdate 方法中访问它们。我的印象是,只要我在同一个应用程序中访问私有文件,我就可以访问它们
    • 在同一个应用程序中,这是真的。在小部件中使用 RemoteViews 时,可能是在不同的进程中访问了 URI。您可以尝试使用MODE_WORLD_READABLE 创建文件。请注意,这在 API 17 中已被弃用,描述中列出了一些替代方案。
    • 事实证明,我只在 API 级别 10 及以下级别使用这整段代码,因此弃用应该不是问题。这似乎已经解决了这个问题,所以谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多