【问题标题】:View image in ACTION_VIEW intent?以 ACTION_VIEW 意图查看图像?
【发布时间】:2018-02-05 15:52:06
【问题描述】:

我想在图像查看器意图中显示我从下一个下载的 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);

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

ERROR/ImageManager(1345): 得到异常解码位图 java.lang.NullPointerException

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

【问题讨论】:

    标签: android


    【解决方案1】:

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

    看起来你的代码没问题,把它和这个 Android sn-p 比较一下:

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

    【讨论】:

    • 如果我想一个接一个地播放更多数量的视频,如何一个接一个地播放视频是可能的意图( , "video/*");
    • @andrewww 好问题,我认为您应该将其作为一个新问题提出。我不确定,但我相信这里有人会知道。不过,他们永远不会在此评论中看到它。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多