[英]Open File With Intent (Android)
我想使用Intent打開任何文件(如果可能的話)。 我定義了openFile(Uri file, String mimeType)
方法,並使用注冊的BroadcastReceiver調用它。 這是方法:
private void openFile(Uri file, String mimeType) {
Intent openFile = new Intent(Intent.ACTION_VIEW);
openFile.setData(file);
try {
context.startActivity(openFile);
} catch (ActivityNotFoundException e) {
Log.i(TAG, "Cannot open file.");
}
}
我目前沒有使用mimeType
,我只是想讓它發揮作用。 在具有Uri content://downloads/all_downloads/3980
的.pdf上調用openFile方法的結果content://downloads/all_downloads/3980
只是在pdf查看器中打開的空白pdf(mime類型可能正確解釋),其中3980 downloadID顯示為文件名。
我知道發生了什么,因為內容Uri無論出於何種原因都沒有得到妥善解決。 我用Uri localUri = downloadManager.getUriForDownloadedFile(downloadId);
行獲取Uri Uri localUri = downloadManager.getUriForDownloadedFile(downloadId);
其中downloadId
是從downloadManager.enqueue(request);
返回的long downloadManager.enqueue(request);
當我有內容類型Uri時如何打開文件?
調用addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
。 否則,其他應用程序將無權使用該Uri
標識的內容。
請注意,這僅在您有權使用該Uri
標識的內容時才有效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.