android – FileProvider – 从下载目录中打开文件

前端之家收集整理的这篇文章主要介绍了android – FileProvider – 从下载目录中打开文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我无法打开“下载文件夹”中的任何文件.

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

  1. DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
  2. request.setDescription(descricao);
  3. request.setTitle(titulo);
  4.  
  5. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
  6. request.allowScanningByMediaScanner();
  7. request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
  8. }
  9. request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,nome);
  10.  
  11. enq = downloadManager.enqueue(request);

在此之后,我的文件在目录文件夹中保存正确:Android>>内部共享存储>>下载.
***我看到这条路径在ubuntu中手动打开设备的高清.由于图像显示路径.
Android HD by ubuntu folder – see the path

我尝试用这个打开这个文件

  1. downloadManager = (DownloadManager)getContext().getSystemService(DOWNLOAD_SERVICE);
  2. BroadcastReceiver receiver = new BroadcastReceiver() {
  3.  
  4. @Override
  5. public void onReceive(Context context,Intent intent) {
  6. String action = intent.getAction();
  7. if(DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
  8. long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID,0);
  9. DownloadManager.Query query = new DownloadManager.Query();
  10. query.setFilterById(enq);
  11. Cursor c = downloadManager.query(query);
  12. if(c.moveToFirst()) {
  13. int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
  14. if(DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {
  15. String uriString = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
  16.  
  17. if (uriString.substring(0,7).matches("file://")) {
  18. uriString = uriString.substring(7);
  19. }
  20.  
  21. File file = new File(uriString);
  22.  
  23. Uri uriFile = FileProvider.getUriForFile(getContext(),BuildConfig.APPLICATION_ID + ".fileprovider",file);
  24. String mimetype = "application/pdf";
  25. Intent myIntent = new Intent(Intent.ACTION_VIEW);
  26. myIntent.setDataAndType(uriFile,mimetype);
  27.  
  28. Intent intentChooser = Intent.createChooser(myIntent,"Choose Pdf Application");
  29. startActivity(intentChooser);
  30. }
  31. }
  32. }
  33. }
  34. };
  35.  
  36. getContext().registerReceiver(receiver,new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

我在清单中声明我的文件提供程序:

  1. <provider
  2. android:name="android.support.v4.content.FileProvider"
  3. android:authorities="${applicationId}.fileprovider"
  4. android:exported="false"
  5. android:grantUriPermissions="true">
  6. <Meta-data
  7. android:name="android.support.FILE_PROVIDER_PATHS"
  8. android:resource="@xml/provider_paths"/>
  9. </provider>

并与此:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <paths xmlns:android="http://schemas.android.com/apk/res/android">
  3. <external-path name="Download" path="Download/"/>
  4. </paths>

但是,当我单击按钮下载时,我收到此消息:“此文件无法访问.请检查位置或网络,然后重试.”

恢复:

1 – 文件已下载并保存在目录文件夹中.

2 – 意图已启动,但文件未打开.

3 – 调试模式在“new File(urlString)”中给出了这个:“urlString = / storage / emulated / 0 / Download / name.pdf”

4 – 在“FileProvider.getUriFromFile …”调试模式具有:

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

谢谢.

解决方法

在与startActivity()和FileProvider Uri一起使用的Intent上调用addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION).没有它,活动无权访问您的内容.

猜你在找的Android相关文章