FileProvider-从下载目录打开文件 [英] FileProvider - Open File from Download Directory
问题描述
我无法从下载文件夹中打开任何文件。
I can't open any file from Download Folder.
我可以下载文件并使用以下方法保存在下载文件夹中:
I can download a file and save in Download Folder with this:
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);
此后,我的文件正确保存在目录文件夹中:Android >> Internal Shared Storage >>下载。
***我看到此路径是在ubuntu中手动打开设备的HD。如图所示为路径。
通过ubuntu文件夹安装Android HD-查看路径
After this, my file is correct saved at Directory Folder: Android >> Internal Shared Storage >> Download. ***This path I see manually opening the device's hd in ubuntu. As the image shows the path. Android HD by ubuntu folder - see the path
然后尝试使用以下命令打开此文件:
And I try open this file with this:
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));
我在清单中声明我的文件提供程序:
I declare my file provider in manifest with this:
<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>
但是当我单击下载按钮时,我收到此消息:无法访问此文件。检查位置或网络,然后重试。
But when I click the button to download I receive this message: "This file coud be not accessed. Check the location or the network and try again."
恢复:
1-已下载文件,然后保存在目录文件夹中。
1 - The file is downloaded and saved at the directory folder.
2-意图已启动,但文件未打开。
2 - The intent is started, but the file is not openned.
3-调试模式在新文件(urlString)处提供给我: urlString = / storage / emulated / 0 / Download / name.pdf
3 - Debug mode give me this at "new File(urlString)": "urlString=/storage/emulated/0/Download/name.pdf"
4-在调试模式为 FileProvider.getUriFromFile ...:
4 - At "FileProvider.getUriFromFile..." debug mode have this:
uriFile = content://com.example.android.parlamentaresapp.fileprovider/Download/name.pdf
"uriFile = content://com.example.android.parlamentaresapp.fileprovider/Download/name.pdf"
谢谢。
推荐答案
致电 startActivity()
一起使用的 Intent
上> addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)和 FileProvider
Uri
。否则,该活动将无权访问您的内容。
Call addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
on the Intent
that you use with startActivity()
and the FileProvider
Uri
. Without that, the activity has no rights to access your content.
这篇关于FileProvider-从下载目录打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!