开发者

Can I use ACTION_VIEW to view a JPG I've downloaded?

I'm having a terrible time trying to view a JPG file that I've downloaded using ACTION_VIEW in my Android App. I can verify that the file is present and that it is the correct size. I can pull the file from the emulator and open it on my computer, so I know the JPG is not corrupt. I've also tried the code in View image in ACTION_VIEW intent?.

My code is as follows:

// 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);

This seems like it should be straight forward, but whenever I run the above code, I get

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.andro开发者_如何学运维id.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)

Any ideas about what's going on here?


As far as I can make out from looking at the platform source code, you are passing in a non-existent file path in your URI.

What is the value of downloadsDirectoryPath? Bet you 15 rep points it's not fully-qualified...


Scott answered his own question in the comments but I'll repost as an answer since it proved helpful to me too: Check the file permissions on the image. I had also pulled the .jpg to verify it was fine, but the real problem was that FileOutputStream() had set the permissions by default so that ACTION_VIEW couldn't access the file. (Personally I'd call this a bug in the viewer that it segfaults instead of logging a lack of permission to read the file, but I digress...)

As per Scott's suggestion, using openFileOutput("test.jpg", MODE_WORLD_READABLE) instead of FileOutputStream() fixes the problem.

The better solution (depending on the application) is probably to use one of these:

http://developer.android.com/guide/topics/data/data-storage.html#filesExternal

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜