Android 在 ACTION_VIEW 意图中查看图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1740654/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:33:49  来源:igfitidea点击:

View image in ACTION_VIEW intent?

android

提问by Mark

I'd like to show a png or jpg I downloaded from the next in an image viewer intent, but can't get it to work.

我想在图像查看器意图中显示我从下一个下载的 png 或 jpg,但无法使其工作。

Bitmap bmp = getImageBitmap(jpg);
String path = getFilesDir().getAbsolutePath() + "/test.png"; 
File file = new File(path); 
FileOutputStream fos = new FileOutputStream(file);
bmp.compress( CompressFormat.PNG, 100, fos ); 
fos.close(); 

Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(path)), "image/png");
startActivity(intent);

I know the bitmap is downloaded ok (use the same routine for supplying it my ImageView instances elsewhere in my app) - I think it wrote to file ok, I can see it on disk and the file size is correct. The intent is launched but an exception is thrown:

我知道位图下载正常(使用相同的例程在我的应用程序的其他地方提供我的 ImageView 实例) - 我认为它写入文件正常,我可以在磁盘上看到它并且文件大小是正确的。启动意图但抛出异常:

ERROR/ImageManager(1345): got exception decoding bitmap java.lang.NullPointerException

错误/图像管理器(1345):得到异常解码位图 java.lang.NullPointerException

then the new activity just sits there, blank. How does this work?

然后新活动就坐在那里,空白。这是如何运作的?

回答by David Glass

Check out Android issue 2092it sounds similar to what you are describing. The issue states, "Bitmap.compress() fails for PNG files saved in Indexed Color Mode (instead of RGB color mode)", however the first commenter thinks that, "it looks like its not an indexed color problem but a PNG problem."

查看Android 问题 2092听起来与您所描述的相似。问题指出,“Bitmap.compress() 对于以索引颜色模式(而不是 RGB 颜色模式)保存的 PNG 文件失败”,但是第一个评论者认为,“它看起来不是索引颜色问题,而是 PNG 问题。 ”

It looks like your code is fine, compare it to this Android snippet:

看起来您的代码很好,将其与此 Android 代码段进行比较:

Intent intent = new Intent();  
intent.setAction(android.content.Intent.ACTION_VIEW);  
File file = new File("/sdcard/test.mp4");  
intent.setDataAndType(Uri.fromFile(file), "video/*");  
startActivity(intent);   

Intent intent = new Intent();  
intent.setAction(android.content.Intent.ACTION_VIEW);  
File file = new File("/sdcard/test.mp3");  
intent.setDataAndType(Uri.fromFile(file), "audio/*");  
startActivity(intent); 

回答by wuntee

Another problem may be permissions on the file. Typically your /data/data/[app]/ directories are not world readable, and are owned by you applications "app_XX" user/group. Either make sure your permissions are correct, or make sure the file is in a place that both applications can read (emms, or sd card)

另一个问题可能是文件的权限。通常,您的 /data/data/[app]/ 目录不是世界可读的,并且由您的应用程序“app_XX”用户/组拥有。确保您的权限正确,或确保文件位于两个应用程序都可以读取的位置(emms 或 sd 卡)