在ACTION_VIEW意图显示图像?意图、图像、ACTION_VIEW

2023-09-12 23:50:05 作者:我本无心何来绝情

我想显示PNG或JPG我的下一个图像查看器下载的意向,但不能让它的工作。

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.

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);

我知道该位图下载OK(使用同样的程序在我的应用程序在其他地方提供它在我的ImageView的情况下) - 我觉得写信给文件就OK了,我可以看到它在磁盘上的文件的大小是正确的。这样做的目的是启动,但会抛出一个异常:

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:

ERROR / ImageManager(1345):有异常解码位图显示java.lang.NullPointerException

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

那么新的活动,只是坐在那里,一片空白。这是如何工作的?

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

推荐答案

查看 Android的问题,2092 的这听起来像你所描述的。这个问题指出,Bitmap.com preSS()失败存储在索引颜色模式的PNG文件(而不是RGB色彩模式),但第一个评论者认为,它看起来像它不是一个索引颜色的问题,但一个PNG的问题。

Check out Android issue 2092 it 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."

它看起来像你的code是很好,把它比作这个Android片段:

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

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);