4

我无法从下载文件夹中打开任何文件。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”

谢谢。

+0

'Android >> Internal Shared Storage >> Download.'。这不是您可以在代码中使用的文件系统路径。请提供实际的完整路径。 – greenapps

回答

6

在您与startActivity()FileProviderUri使用Intent呼叫addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)。没有这一点,该活动无权访问您的内容。

+0

非常感谢。解决了我的问题! – Sarara

+0

非常感谢。失踪的旗帜是问题! –

+0

感谢,它适用于我,但接收应用程序(惊人的文本编辑器)显示'sh: [7]:cat:/Download/error.txt:没有这样的文件或目录' –