[英]Open an image from the external storage
我已經嘗試了幾個小時,使用Intent.ACTION_VIEW
從Android的外部存儲中打開圖像。 正如您可能已經知道的那樣,我仍然無法做到這一點。
我在Google各處進行了搜索,並嘗試了很多方法來完成這項工作,但是沒有一個方法能做到。 我所能找到的都是長達1年的問題。 我嘗試使用FileProvider
,最終得到的是:
File imagePath = new File(Environment.getExternalStorageDirectory(), "LyceeLamartine/cache");
File newFile = new File(imagePath, "calendrier.png");
Uri contentUri = FileProvider.getUriForFile(MainActivity.this, "ml.toufic.lyceelamartine.fileprovider", newFile);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(contentUri, "image/*");
startActivity(intent);
上面的代碼成功打開了Google相冊,但顯示黑屏,其路徑似乎為: content://ml.toufic.lyceelamartine.fileprovider/name/calendrier.png
,這似乎是內部存儲路徑。
我要訪問的圖像存儲在:“ / storage / emulated / 0 / LyceeLamartine / cache / calendrier.png”,並且我正在使用具有Android O(8.0)的手機。
我仍然是Java的初學者,任何幫助將不勝感激!
路徑似乎是:content://ml.toufic.lyceelamartine.fileprovider/name/calendrier.png,這似乎是內部存儲路徑。
那不是一條路。 那是一個Uri
。 它有一個方案( content
)。 它指向您的FileProvider
( ml.toufic.lyceelamartine.fileprovider
)。 看起來很好-如果Uri
出現問題,則FileProvider
在嘗試創建它時會引發異常。
上面的代碼成功打開了Google相冊,但顯示黑屏
您的Intent
有兩個問題。
首先,不要使用通配符MIME類型。 這是您的文件。 您知道什么是MIME類型。 使用實際的MIME類型(在本例中為image/png
)。
其次,在調用startActivity()
之前,在Intent
上調用addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
作為設置它的一部分。 目前,沒有任何應用有權使用您的內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.