2017-12-18 84 views
1

我已經配置了一個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); 
+0

您正在測試哪種Android版本?你是否看到來自LogCat中這些其他應用的特定堆棧跟蹤,你可以添加到你的問題中(因爲它們可能提供線索)?你確定該文件存在嗎?由於'.bak'文件不會有標準的MIME類型,您是否嘗試過使用'application/octet-stream'? – CommonsWare

+0

我的確在使用application/octet-stream,正如您從logcat輸出中看到的那樣:)在這種情況下,我無法從Google Drive應用程序中看到任何堆棧跟蹤,這些情況會將我帶到任何地方。我使用運行Android 8.0和8.1的模擬器進行測試。我還可以確認在上面的代碼中定義爲backupFile的文件存在。 – ardevd

+0

我可能會將一個支持ACTION_SEND的簡單*獨立*應用程序放在一起,用於應用程序/ octet-stream,然後使用該應用程序查看此通信客戶端的生活情況。如果該應用可以成功打開流並消耗字節,那麼Google Drive和其他人應該可以做同樣的事情(儘管這是「ACTION_SEND」測試是單獨應用的關鍵原因,而不僅僅是一些活動在你自己的應用程序中)。 – CommonsWare

回答

1

變化:

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派生它是沒有意義的。

-2

我認爲你必須設置缺少的類型。嘗試添加如下:

intent.setType("image/*|application/pdf|audio/*"); 

更多詳情,請訪問https://developer.android.com/training/sharing/send.html

+0

首先,問題中的代碼已經通過'setDataAndType()'設置了類型。其次,'setType()'不支持管道分隔的MIME類型。 – CommonsWare

+0

的確,這是通過intent.setDataAndType()完成的 – ardevd