gpt4 book ai didi

android - FileProvider - 从下载目录打开文件

转载 作者:可可西里 更新时间:2023-11-01 19:05:32 27 4
gpt4 key购买 nike

我无法打开下载文件夹中的任何文件。

我可以下载一个文件并保存在下载文件夹中:

   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 >> 内部共享存储 >> 下载。***这个路径我在ubuntu中看到手动打开设备的hd。如图所示路径。 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));

我在 list 中声明了我的文件提供者:

    <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 - Intent 已启动,但文件未打开。

3 - Debug模式在“new File(urlString)”处给我这个:“urlString=/storage/emulated/0/Download/name.pdf”

4 - 在“FileProvider.getUriFromFile...” Debug模式下有这个:

"uriFile = content://com.example.android.parlamentaresapp.fileprovider/Download/name.pdf"

谢谢。

最佳答案

Intent 上调用 addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)startActivity()FileProvider URI。否则, Activity 无权访问您的内容。

关于android - FileProvider - 从下载目录打开文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43103586/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com