Sav*_*sis 6 java android android-intent
我有一个图像的网址.我需要做的是使用intent为图像启动默认图像查看器.
我尝试使用以下方式启动它:
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);
Run Code Online (Sandbox Code Playgroud)
但它不起作用.如果我没有指定数据类型,则intent会启动浏览器,因为数据是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);
Run Code Online (Sandbox Code Playgroud)
编辑2:我决定我可能需要下载的图像,所以我决定先将它们保存在SD卡上.这造成了其他问题.我问了一个新问题,因为这是一个不同的问题.
也许画廊要求将图像保存到内存中。
为什么不将图像下载到位图中,然后将其另存为文件,然后在该文件上启动图像意图?
(如果您不希望文件随后保留,您可以将其保存在应用程序数据目录中)
| 归档时间: |
|
| 查看次数: |
14867 次 |
| 最近记录: |