//下載按鈕
downloadPdf2LocalFile((fp) -> {
File file = new File(fp);
copyFile2PublicDownload(file);
Log.d(TAG, "getParentFile: "+file.getParentFile().getPath());
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = FileProvider.getUriForFile(getApplicationContext(),"com.twx.poc.voicejob.FileProvider",file.getParentFile());
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(uri, "*/*");
startActivity(intent);
});
上面代碼是將檔案拷貝至Download目錄,然后打開intent讓用戶查看,結果并沒有跳轉至下載目錄
在網路上看了很多帖子都沒解決。特請大佬開路^_^
uj5u.com熱心網友回復:
官方檔案找到這么一段描述: https://developer.android.com/training/data-storage/shared/documents-files#grant-access-directory
public void openDirectory(Uri uriToLoad) {
// Choose a directory using the system's file picker.
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
// Provide read access to files and sub-directories in the user-selected
// directory.
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
// Optionally, specify a URI for the directory that should be opened in
// the system file picker when it loads.
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, uriToLoad);
startActivityForResult(intent, your-request-code);
}
應用到代碼中
// Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
// Uri uri = FileProvider.getUriForFile(getApplicationContext(),"com.twx.poc.voicejob.FileProvider",file.getParentFile());
// intent.addCategory(Intent.CATEGORY_OPENABLE);
// intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
// intent.setDataAndType(uri, "*/*");
// startActivity(intent);
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
可以正常打開了。
不知道有沒有兼容性問題
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/234496.html
標籤:Android
上一篇:(新手求助)SIOSocket github專案跑不起來
下一篇:IPA
