我無法從下載文件夾中打開任何文件。FileProvider - 從下載目錄中打開文件
我可以下載一個文件,並在下載文件夾保存與此:
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDescription(descricao);
request.setTitle(titulo);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, nome);
enq = downloadManager.enqueue(request);
在此之後,我的文件是正確保存在目錄文件夾:安卓>>內部的Shared Storage >>下載。 ***我看到這個路徑在Ubuntu下手動打開設備的高清。如圖所示顯示路徑。 Android HD by ubuntu folder - see the path
我嘗試打開此文件與此:
downloadManager = (DownloadManager)getContext().getSystemService(DOWNLOAD_SERVICE);
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(enq);
Cursor c = downloadManager.query(query);
if(c.moveToFirst()) {
int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
if(DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {
String uriString = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
if (uriString.substring(0, 7).matches("file://")) {
uriString = uriString.substring(7);
}
File file = new File(uriString);
Uri uriFile = FileProvider.getUriForFile(getContext(), BuildConfig.APPLICATION_ID + ".fileprovider", file);
String mimetype = "application/pdf";
Intent myIntent = new Intent(Intent.ACTION_VIEW);
myIntent.setDataAndType(uriFile, mimetype);
Intent intentChooser = Intent.createChooser(myIntent, "Choose Pdf Application");
startActivity(intentChooser);
}
}
}
}
};
getContext().registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
我宣佈我的文件提供的清單與此:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
以及與此:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="Download" path="Download/"/>
</paths>
但是當我點擊按鈕下載時,我收到了這條消息:「這個文件不能訪問。檢查位置或網絡,然後再試一次「
恢復:
1 - 文件被下載並保存在目錄文件夾
2 - 這樣做的目的是啓動,但該文件不是。 openned
3 - 調試模式給我這個在 「新文件(urlString)」: 「urlString = /存儲/模擬/ 0 /下載/ name.pdf」
4 - 在「FileProvider.getUriFromFile。 ..「調試模式有這個:
「uriFile = content://com.example.android.parlamentaresapp.fileprovider/Download/name.pdf」
謝謝。
'Android >> Internal Shared Storage >> Download.'。這不是您可以在代碼中使用的文件系統路徑。請提供實際的完整路徑。 – greenapps