我可以使用ACTION_VIEW查看我已下载的JPG吗?
问题描述:
我正在尝试查看我在Android应用程序中使用ACTION_VIEW下载的JPG文件。我可以验证文件是否存在,并且它是正确的大小。我可以从模拟器中拉出文件并在计算机上打开它,所以我知道JPG并未损坏。我也尝试了View image in ACTION_VIEW intent?中的代码。我可以使用ACTION_VIEW查看我已下载的JPG吗?
我的代码如下:
// start intent to view the file
String filename = "test.jpg"
String downloadsDirectoryPath = getFilesDir().getPath() + "/downloads";
File file = new File(downloadsDirectoryPath + "/" + filename);
Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(file), "image/*");
startActivity(i);
这似乎应该是简单的,但每当我运行上面的代码,我得到
ERROR/UriImage(336): got exception decoding bitmap
ERROR/UriImage(336): java.lang.NullPointerException
ERROR/UriImage(336): at com.android.camera.Util.makeInputStream(Util.java:336)
ERROR/UriImage(336): at com.android.camera.Util.makeBitmap(Util.java:307)
ERROR/UriImage(336): at com.android.camera.Util.makeBitmap(Util.java:299)
ERROR/UriImage(336): at com.android.camera.gallery.UriImage.fullSizeBitmap(UriImage.java:94)
ERROR/UriImage(336): at com.android.camera.gallery.UriImage.fullSizeBitmap(UriImage.java:86)
ERROR/UriImage(336): at com.android.camera.gallery.UriImage.thumbBitmap(UriImage.java:120)
ERROR/UriImage(336): at com.android.camera.ImageGetter$ImageGetterRunnable.executeRequest(ImageGetter.java:173)
ERROR/UriImage(336): at com.android.camera.ImageGetter$ImageGetterRunnable.run(ImageGetter.java:149)
ERROR/UriImage(336): at java.lang.Thread.run(Thread.java:1096)
什么是怎么回事任何想法?
答
就我所看到的平台源代码而言,您可能会在URI中传递一个不存在的文件路径。
downloadsDirectoryPath的值是什么?打赌你15点代表它不完全合格...
答
斯科特在评论中回答了他自己的问题,但我会重新发布作为答案,因为它也证明对我有帮助:检查图像上的文件权限。我也拉了.jpg来验证它是否正常,但真正的问题是FileOutputStream()默认设置了权限,因此ACTION_VIEW无法访问该文件。 (我个人认为这是一个浏览器中的缺陷,它代码段错误,而不是记录缺少读取文件的权限,但我离题了......)
按照Scott的建议,使用openFileOutput(“test.jpg “,MODE_WORLD_READABLE)而不是FileOutputStream()修复了这个问题。
更好的解决方案(取决于应用程序)可能是使用其中的一个:
http://developer.android.com/guide/topics/data/data-storage.html#filesExternal
你加android.permission.INTERNET对你的AndroidManifest.xml – 2011-01-25 08:35:06
感谢您的评论。是的,我在清单中有这个。我实际上可以验证该文件是否存在于仿真器中。我弄清楚我的问题与权限有关,而不是与jpg文件相关。请参阅我对其他答案的回答以获得解释。 – 2011-01-25 19:01:16