Launch Intent Viewer to display image from URL

I have an image url. I need to start the default image viewer using intent.

I tried to run it 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 that will not work. If I do not specify the data type, the target launches the browser, since the data is a URL. It works mostly (since you can see the image in the browser), but I would like the gallery to display the image for me.

I can also load the image into a bitmap, but I still don’t know how to display Bitmap using the gallery (if possible). Any ideas?

EDIT: I tried to save the bitmap to the cache, and then ran the viewer in this file, but it does not work. Can you spot any errors in my code? (The Utilities class is the class I wrote. The method just creates a Bitmap. It works, this is not a 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 might need the downloaded images, so I decided to save them to the SD card first. This created other problems. I asked a new question as this is a different issue.

+6
java android android-intent
source share
3 answers

Perhaps the gallery requires that the image be stored in memory.

Why not load the image into a bitmap, and then save it as a file, and then run the image in this file?

(If you do not want the file to remain after that, you can simply save it in the application data directory)

+1
source share

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

+1
source share

You can try this code for gridview onItemClickListner to display in App gallery by default

 OnItemClickListener myOnItemClickListener = new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, Intent it = new Intent(Intent.ACTION_VIEW); it.setDataAndType(Uri.parse("file://" + (String) parent.getItemAtPosition(position)), "image/*"); startActivity(it); } }; 

this is work for me :)

0
source share

All Articles