簡體   English   中英

在ACTION_VIEW意圖中查看圖像?

[英]View image in ACTION_VIEW intent?

我想在圖像查看器意圖中顯示我從下一個下載的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實例) - 我認為它寫入文件確定,我可以在磁盤上看到它並且文件大小正確。 意圖啟動但拋出異常:

ERROR / ImageManager(1345):異常解碼位圖java.lang.NullPointerException

然后新活動就在那里,空白。 這是如何運作的?

查看Android問題2092,它聽起來與您所描述的相似。 該問題指出,“Bitmap.compress()對於索引顏色模式(而不是RGB顏色模式)中保存的PNG文件失敗”,但第一位評論者認為,“它看起來不是索引顏色問題而是PNG問題。 “

看起來您的代碼很好,將其與此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); 

另一個問題可能是文件的權限。 通常,您的/ data / data / [app] /目錄不是全局可讀的,並且由您的應用程序“app_XX”用戶/組擁有。 確保您的權限是正確的,或確保文件位於兩個應用程序都可以讀取的位置(emms或SD卡)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM