java 启动意图查看器以显示来自 url 的图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2951616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Launch intent viewer to display image from url
提问by Savvas Dalkitsis
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.
但它不起作用。如果我不指定数据类型,则意图会启动浏览器,因为数据是一个 url。它基本上有效(因为您可以在浏览器上看到图像)但我想要的是让画廊为我显示图像。
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)
编辑:我尝试将位图保存到缓存,然后在该文件上启动查看器,但它不起作用。你能发现我的代码有什么错误吗?(Utilities 类是我写的一个类。该方法只是创建了 Bitmap。它有效,这不是问题)
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 questionfor that since it is a different problem.
编辑 2:我决定毕竟我可能需要下载的图像,所以我决定首先将它们保存在 SD 卡上。这造成了其他问题。我为此提出了一个新问题,因为这是一个不同的问题。
回答by HXCaine
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)
(如果您不希望该文件之后留下来,您可以将其保存在您的应用程序数据目录中)
回答by Adrian Spinei
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.
您的代码很好,但您需要将图像保存在 SD 卡(“临时”文件)上,以便照片查看器正常工作。在 1.5、1.6、2.1 上测试。
回答by Bilal Mustafa
You can try this code for gridview onItemClickListner to show in default gallery App
您可以尝试将此代码用于 gridview onItemClickListner 以显示在默认图库应用程序中
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 :)
这对我有用:)

