FileProvider:发送文件时没有数据

ard*_*evd 4 android

我已经配置了一个 FileProvider 并且我正在尝试发送文件,但是处理意图的外部应用程序(Google Drive 等)会抛出一个错误,指出该意图不包含任何数据。我在这里缺少什么?

            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);
            }
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

内容 URI 对我来说看起来不错,但由于某种原因它不起作用。有任何想法吗?

这是提供程序路径。

<paths>
    <files-path name="backups" path="backups/" />
</paths>
Run Code Online (Sandbox Code Playgroud)

最后,提供者声明。

        <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>
Run Code Online (Sandbox Code Playgroud)

更新: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
Run Code Online (Sandbox Code Playgroud)

编辑 2:添加它使它工作得很好:

intent.putExtra(Intent.EXTRA_STREAM, contentUri);
Run Code Online (Sandbox Code Playgroud)

Com*_*are 6

改变:

intent.setDataAndType(contentUri, getContext().getContentResolver().getType(contentUri));
Run Code Online (Sandbox Code Playgroud)

到:

intent.setType(contentUri, "application/octet-stream");
intent.putExtra(Intent.EXTRA_STREAM, contentUri);
Run Code Online (Sandbox Code Playgroud)

ACTION_SEND使用EXTRA_STREAM- 不是的Uri方面Intent- 通过Uri. 并且由于您知道 MIME 类型,因此使用 IPC 调用通过ContentResolver.