无法在 Android 中打开图像文件 - 文件未找到异常
我的设备中安装了此应用)。 如果您使用此应用程序,它会缓存一些图像。这就是为什么它在我的设备图库中显示缓存的图像。
我使用光标获取路径
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://
可以检查文件是否存在。
接下来,我想在图库中打开该文件。
- 我应该使用这个路径来打开图像吗? 但这不起作用:
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);
- 或者我应该尝试从 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.
- 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);
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将
new File("file://" + path)
更改为new File(path)
Change
new File("file://" + path)
to justnew File(path)