Launching Intent.ACTION_VIEW intent not working on saved image file

Posted by Savvas Dalkitsis on Stack Overflow See other posts from Stack Overflow or by Savvas Dalkitsis
Published on 2010-06-02T02:17:27Z Indexed on 2010/06/02 2:23 UTC
Read the original article Hit count: 608

Filed under:
|
|
|

First of all let me say that this questions is slightly connected to another question by me. Actually it was created because of that.

I have the following code to write a bitmap downloaded from the net to a file in the sd card:

// Get image from url
URL u = new URL(url);
HttpGet httpRequest = new HttpGet(u.toURI());
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
InputStream instream = bufHttpEntity.getContent();
Bitmap bmImg = BitmapFactory.decodeStream(instream);
instream.close();

// Write image to a file in sd card
File posterFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Android/data/com.myapp/files/image.jpg");
posterFile.createNewFile();
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(posterFile));
Bitmap mutable = Bitmap.createScaledBitmap(bmImg,bmImg.getWidth(),bmImg.getHeight(),true);
mutable.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();

// Launch default viewer for the file
Intent intent = new Intent();                   
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(posterFile.getAbsolutePath()),"image/*");
((Activity) getContext()).startActivity(intent);

A few notes. I am creating the "mutable" bitmap after seeing someone using it and it seems to work better than without it. And i am using the parse method on the Uri class and not the fromFile because in my code i am calling these in different places and when i am creating the intent i have a string path instead of a file.

Now for my problem. The file gets created. The intent launches a dialog asking me to select a viewer. I have 3 viewers installed. The Astro image viewer, the default media gallery (i have a milstone on 2.1 but on the milestone the 2.1 update did not include the 3d gallery so it's the old one) and the 3d gallery from the nexus one (i found the apk in the wild).

Now when i launch the 3 viewers the following happen:

  • Astro image viewer: The activity launches but i see nothing but a black screen.

  • Media Gallery: i get an exception dialog shown "The application Media Gallery (process com.motorola.gallery) has stopped unexpectedly. Please try again" with a force close option.

  • 3D gallery: Everything works as it should.

When i try to simply open the file using the Astro file manager (browse to it and simply click) i get the same option dialog but this time things are different:

  • Astro image viewer: Everything works as it should.

  • Media Gallery: Everything works as it should.

  • 3D gallery: The activity launches but i see nothing but a black screen.

As you can see everything is a complete mess. I have no idea why this happens but it happens like this every single time. It's not a random bug.

Am i missing something when i am creating the intent? Or when i am creating the image file? Any ideas?

© Stack Overflow or respective owner

Related posts about java

Related posts about android