【发布时间】:2017-12-18 20:33:50
【问题描述】:
我已经配置了一个 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);
}
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>
更新: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
编辑 2:添加它可以正常工作:
intent.putExtra(Intent.EXTRA_STREAM, contentUri);
【问题讨论】:
-
您在哪个版本的 Android 上进行测试?您是否在 LogCat 中看到来自这些其他应用程序的特定堆栈跟踪,您可以将这些跟踪添加到您的问题中(因为它们可能会提供线索)?你确定那个文件存在吗?由于
.bak文件没有标准的MIME 类型,您是否尝试过使用application/octet-stream? -
正如您从 logcat 输出中看到的那样,我确实在使用 application/octet-stream :) 在这种情况下,我看不到来自 Google Drive 应用程序的任何堆栈跟踪,这会引导我到任何地方。我使用运行 Android 8.0 和 8.1 的模拟器对此进行了测试。我也可以确认上面代码中定义为backupFile的文件存在。
-
我可能会将一个支持
ACTION_SEND的简单独立应用程序放在一起,然后使用该应用程序来查看客户端的生活是什么样的这种通讯。如果该应用程序可以成功打开一个流并消耗字节,那么 Google Drive 和其他人应该能够做同样的事情(尽管这是让ACTION_SEND测试成为一个单独的应用程序的关键原因,而不仅仅是一些活动在您自己的应用中)。 -
请查看我更新的帖子。最后的 logcat 输出有什么告诉你的吗?我不知道在发送文件时我必须指定 EXTRA_STREAM。
-
呃。对我来说,在 Stack Overflow 上提供帮助还为时过早——我不敢相信我错过了这一点。见my answer。
标签: android