FileProvider:发送文件时没有数据
我已经配置了一个FileProvider,我试图发送文件,但处理意图的外部应用程序(Google Drive等)会抛出一个错误,表明该意图中不包含任何数据。我在这里错过了什么?FileProvider:发送文件时没有数据
  File backupPath = new File(getContext().getFilesDir(), "backups");       File backupFile = new File(backupPath, clickedBackup.getFilename()); 
      Uri contentUri = getUriForFile(getContext(), "com.test.app.fileprovider", backupFile); 
      // create new Intent 
      Intent intent = new Intent(Intent.ACTION_SEND); 
      // set flag to give temporary permission to external app to use your FileProvider 
      intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
      intent.setDataAndType(
        contentUri, 
        getContext().getContentResolver().getType(contentUri)); 
      // validate that the device can open your File! 
      PackageManager pm = getActivity().getPackageManager(); 
      if (intent.resolveActivity(pm) != null) { 
       startActivity(intent); 
      } 
logcat中显示以下内容:
12-18 12:47:55.554 1698 2490 I ActivityManager: START u0 {act=android.intent.action.SEND dat=content://com.test.app.fileprovider/backups/20171918_121910.bak typ=application/octet-stream flg=0x3000001 cmp=com.google.android.apps.docs/.shareitem.UploadMenuActivity} from uid 10079 内容URI对我来说很好,但由于某种原因,它不工作。有任何想法吗?
这里是提供者路径。
<paths>     <files-path name="backups" path="backups/" /> 
</paths> 
最后,提供者声明。
 <provider       android:name="android.support.v4.content.FileProvider" 
      android:authorities="com.test.app.fileprovider" 
      android:grantUriPermissions="true" 
      android:exported="false"> 
      <meta-data 
       android:name="android.support.FILE_PROVIDER_PATHS" 
       android:resource="@xml/exposed_filepaths" /> 
     </provider> 
UPDATE:logcat中也显示出其可能是或不是关键问题就在这里
12-18 13:21:23.739 4818 6764 E DataSourceHelper: Uploading single file but neither EXTRA_STREAM nor EXTRA_TEXT is specified. 12-18 13:21:23.790 1431 1483 D gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 8298496 
12-18 13:21:23.796 1431 1483 I chatty : uid=1000(system) HwBinder:1431_1 identical 1 line 
12-18 13:21:23.807 1431 1483 D gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 8298496 
12-18 13:21:23.824 4818 4818 E UploadMenuActivity: No files requested to be uploaded: android.intent.action.SEND 
EDIT 2如下:添加这使得它工作得很好:
intent.putExtra(Intent.EXTRA_STREAM, contentUri); 回答:
变化:
intent.setDataAndType(contentUri, getContext().getContentResolver().getType(contentUri)); 到:
intent.setType(contentUri, "application/octet-stream"); intent.putExtra(Intent.EXTRA_STREAM, contentUri); 
ACTION_SEND使用EXTRA_STREAM —不是Uri方面的的—用于通过Uri发送。而且由于您知道MIME类型,因此使用IPC调用通过ContentResolver派生它是没有意义的。
回答:
我认为你必须设置缺少的类型。尝试添加如下:
intent.setType("image/*|application/pdf|audio/*"); 更多详情,请访问https://developer.android.com/training/sharing/send.html
以上是 FileProvider:发送文件时没有数据 的全部内容, 来源链接: utcz.com/qa/263102.html








