无法在 Android 中打开图像文件 - 文件未找到异常

发布于 2024-11-26 12:21:42 字数 2012 浏览 2 评论 0原文

我的设备中安装了应用)。 如果您使用此应用程序,它会缓存一些图像。这就是为什么它在我的设备图库中显示缓存的图像。

我使用光标获取路径

final String[] columns = { MediaStore.Images.Media.DATA };
Cursor imagecursor = managedQuery(
        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns,
        MediaStore.Images.Media._ID + " = " + id, null, MediaStore.Images.Media._ID);
if (imagecursor != null && imagecursor.getCount() > 0){
    imagecursor.moveToPosition(0);
    String path = imagecursor.getString(imagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
    Log.d("path", path);
    File f = new File("file://" + path);
    Log.d("check_file", "" + f.exists());
}

我获取路径:

/mnt/sdcard/cache/hk.ignition.podcast/cartoon/2/http%3A%2F%2Fbigcity.learnenglishapps.com%2Fimages%2Fcartoons%2Fset2%2Ffast-food-new-1.png

下载的图像是: http://bigcity.learnenglishapps.com/images/cartoons /set2/fast-food-new-1.png

当我提取文件或使用 Astro 等文件管理器浏览它时,它就在那里。 但是当我检查文件是否存在时,它说false

人品有问题吗?

更新:

删除file://可以检查文件是否存在。

接下来,我想在图库中打开该文件。

  1. 我应该使用这个路径来打开图像吗? 但这不起作用:
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    intent.setDataAndType(
                Uri.parse("file:///mnt/sdcard/cache/hk.ignition.podcast/cartoon/2/http%3A%2F%2Fbigcity.learnenglishapps.com%2Fimages%2Fcartoons%2Fset2%2Ffast-food-new-1.png"),
                "image/*");
    startActivityForResult(intent, 3);
  1. 或者我应该尝试从 MediaStore 获取 content:// 样式路径?

更新2:

使用:

Uri.fromFile(f);

而不是Uri.Parse()解决。因为它从编码格式构建 / 字符。

I have this app installed in my device (Nexus s).
It caches some images if you use this app. This is why it shows cached images in my device's gallery.

I use cursor to get the path

final String[] columns = { MediaStore.Images.Media.DATA };
Cursor imagecursor = managedQuery(
        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns,
        MediaStore.Images.Media._ID + " = " + id, null, MediaStore.Images.Media._ID);
if (imagecursor != null && imagecursor.getCount() > 0){
    imagecursor.moveToPosition(0);
    String path = imagecursor.getString(imagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
    Log.d("path", path);
    File f = new File("file://" + path);
    Log.d("check_file", "" + f.exists());
}

I get the path:

/mnt/sdcard/cache/hk.ignition.podcast/cartoon/2/http%3A%2F%2Fbigcity.learnenglishapps.com%2Fimages%2Fcartoons%2Fset2%2Ffast-food-new-1.png

Downloaded image is:
http://bigcity.learnenglishapps.com/images/cartoons/set2/fast-food-new-1.png

When I pull the file or explore it with file manager like Astro, its there.
But when I check if file exist, then it says false.

Is there any character issue?

Update:

Removing file:// worked to check if file exist.

Next is, I'd like to open that file in gallery.

  1. should I use this path to open image?
    But that not worked:
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    intent.setDataAndType(
                Uri.parse("file:///mnt/sdcard/cache/hk.ignition.podcast/cartoon/2/http%3A%2F%2Fbigcity.learnenglishapps.com%2Fimages%2Fcartoons%2Fset2%2Ffast-food-new-1.png"),
                "image/*");
    startActivityForResult(intent, 3);
  1. Or I should try to get content:// style path from MediaStore?

Update 2:

Solved using :

Uri.fromFile(f);

Instead of Uri.Parse(). As it builds / character from encode format.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小嗲 2024-12-03 12:21:42

new File("file://" + path) 更改为 new File(path)

Change new File("file://" + path) to just new File(path)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文