views:

384

answers:

3

I have the url of an image. What i need to do is launch the default image viewer for images using an intent.

I tried launching it by using:

Uri uri = Uri.parse("http://www.google.com/intl/en_ALL/images/srpr/logo1w.png");  
Intent it = new Intent(Intent.ACTION_VIEW);
it.setDataAndType(uri, "image/*")
startActivity(it);

But it doesn't work. If I do not specify the type of data, the intent launches the browser since the data is a url. It works basically (since you can see the image on the browser) but what I would like is to have the gallery display the image for me.

I can also download the image into a Bitmap but I would still not know how to display the Bitmap using the gallery (if that's even possible). Any ideas?

EDIT: I tried saving the bitmap to the cache and then launch the viewer on that file but it doesn't work. Can you spot any mistakes on my code? (The Utilities class is a class i wrote. The method simply creates the Bitmap. It works, that's not the problem)

File temp = File.createTempFile("tempImage", ".jpg", getContext().getCacheDir());
Bitmap bmp = Utilities.loadBitmap(largeUrl.toString());
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(temp));
bmp.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.close();
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(temp), "image/jpg");
((Activity) getContext()).startActivity(intent);

EDIT 2: I decided that i may need the downloaded images after all so i decided to first save them on the sd card. That created other problems. I asked a new question for that since it is a different problem.

+1  A: 

Perhaps the gallery requires that the image is saved to memory.

Why not download the image into a bitmap and then save that as a file, then fire up the image intent on that file?

(If you don't want the file to stick around afterwards, you could just save it in your app data directory)

HXCaine
I would like to be able to do it without saving stuff but i tried it and it doesn't work. Can you please take a look at my edited question and spot any mistakes?
Savvas Dalkitsis
Two comments: (1) You do setDataAndType on the temp file, before it has been output. Why not retrieve the saved file and then add that to the intent? (2) When you say "it doesn't work", what is the result or exception?
HXCaine
what do you mean before it has been output? i compress the bitmap, close the output stream and then create the intent...
Savvas Dalkitsis
I just meant that you should see if retrieving the file from the cache directory works (instead of using that 'temp' variable). Try retrieving it after out.close() and send that to the intent instead. Also, you didn't answer my second question. What's currently happening when you try the above code?
HXCaine
i created a spin-off question based on that in which i am posting the adb logcat output of the exception. Also i have changed the way i am creating the image file (instead of encoding a Bitmap, i am simply downloading the jpeg file from the server). Try the link i have included in this question to see the other one, if you can help that'd be great.
Savvas Dalkitsis
A: 

I answered my own spin off question.

Savvas Dalkitsis
+1  A: 

Your code is fine but you need to save the image on the sd card ("temp" File) for the photo viewer to work. Tested on 1.5, 1.6, 2.1.

aspinei