【发布时间】:2010-06-01 16:43:52
【问题描述】:
我有一张图片的网址。我需要做的是使用意图启动图像的默认图像查看器。
我尝试使用以下方式启动它:
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);
但它不起作用。如果我没有指定数据类型,则意图启动浏览器,因为数据是一个 url。它基本上可以工作(因为您可以在浏览器上看到图像),但我想让画廊为我显示图像。
我也可以将图像下载到位图中,但我仍然不知道如何使用图库显示位图(如果可能的话)。有什么想法吗?
编辑:我尝试将位图保存到缓存中,然后在该文件上启动查看器,但它不起作用。你能在我的代码中发现任何错误吗? (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);
编辑 2:我决定我可能需要下载的图像,所以我决定先将它们保存在 sd 卡上。这产生了其他问题。我问了new question,因为这是一个不同的问题。
【问题讨论】:
标签: java android android-intent