如何设置使用默认 Android 查看器查看保存在内部存储中的图像的意图?
我想做的是从网络下载图像(它是 GIF 格式,如果这会改变任何内容)并将其显示在具有缩放/平移功能的屏幕上。我已成功将图像下载到 Bitmap 实例 myBitmap 中,但 ImageView 没有缩放功能。因此,我愿意为它提供具有缩放/平移功能的默认查看器。为了设置意图,我需要提供保存文件的 URI。 因此,首先我将文件保存到内部存储中(还可以使用 MODE_WORLD_READABLE 访问其他应用程序):
try {
FileOutputStream out = openFileOutput("scrible",
Context.MODE_WORLD_READABLE);
myBitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
} catch (FileNotFoundException e) {
Log.d(myTag, e.toString());
}
然后我成功检查其真正可读:
File myFile = new File(getFilesDir(), "scrible");
if (myFile.canRead()) {
Log.d(myTag, "Its readable");
}
然后我尝试设置意图(这给了我错误:意外停止):
Uri fileUri = Uri.fromFile(myFile);
startActivity(new Intent(Intent.ACTION_VIEW, fileUri));
我尝试设置单独启动意图,并且在调用 startActivity(intent); 之前不会给出错误;
我做错了什么?
What I am trying to do is to download image from web (its in GIF format, if that changes anything) and show it on screen with zoom/pan capability. I've successfully downloaded image into Bitmap instance myBitmap, but ImageView doesnt have zooming feature. So instead I'm willing to present it with default viewer which has zoom/pan features. In order to set up intent I need to provide URI to saved file.
So first I save file into internal storage (also giving access to other apps withe MODE_WORLD_READABLE):
try {
FileOutputStream out = openFileOutput("scrible",
Context.MODE_WORLD_READABLE);
myBitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
} catch (FileNotFoundException e) {
Log.d(myTag, e.toString());
}
Then I successfully check its really readable:
File myFile = new File(getFilesDir(), "scrible");
if (myFile.canRead()) {
Log.d(myTag, "Its readable");
}
Then I try to set up intent (which gives me error: stopped unexpectedly):
Uri fileUri = Uri.fromFile(myFile);
startActivity(new Intent(Intent.ACTION_VIEW, fileUri));
I've tried to set up intent separately and it doesnt give error antil call to startActivity(intent);
What I am doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下对我来说效果很好:
我看到的区别是您没有调用 setDataAndType 。
The following works well for me:
The difference that I see is that you do not call the
setDataAndType
.