2

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.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)

Any ideas about what's going on here?

Community
  • 1
  • 1
Scott H
  • 21
  • 2
  • Did you add android.permission.INTERNET in your AndroidManifest.xml – Tanmay Mandal Jan 25 '11 at 08:35
  • Thanks for the comment. Yes, I have that in the manifest. I can actually verify that the file is there in the emulator. I did figure out that my problem was related to permissions and not the jpg file. See my response to the other answer for an explanation. – Scott H Jan 25 '11 at 19:01

2 Answers2

0

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...

Reuben Scratton
  • 38,595
  • 9
  • 77
  • 86
  • 1
    Thanks for the answer. Turns out that the downloadsDirectoryPath is good, but I did figure out the problem. Since I used FileOutputStream() instead of openFileOut(), the file was only readable by me (owner). So, when I ditched the FileOutputStream() and used openFileOutput("test.jpg", MODE_WORLD_READABLE), it worked. I was too focused on what was wrong with the JPG and not enough on whether ACTION_VIEW could read it. I wanted to use subdirectories in my internal storage, but I probably can't do that. The correct solution is probably to move external storage or make a Content Provider. – Scott H Jan 25 '11 at 18:57
  • Ain't no shame in answering your own question. Post the solution in an answer and accept it. Solution worked for me. – cesarislaw Jun 17 '11 at 20:25
0

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

Brandyn
  • 430
  • 3
  • 9