Android端向Google Drive上传文件夹报错:请求无数据
问题描述
我在移动设备上尝试将文件夹分享至Google Drive时遇到错误:上传失败,来自移动应用的Google Drive请求未包含数据。该文件夹可以正常通过WhatsApp分享。我的代码如下:
File directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); File folderData = new File(directory + "/Stocks/" + "Branch/" + "Test"); String folderPath = folderData.getPath(); Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE); intent.setType("*/*"); // Get the list of files in the folder File folder = new File(folderPath); File[] files = folder.listFiles(); // Create an ArrayList to hold the file Uri objects ArrayList<Uri> fileUris = new ArrayList<>(); // Loop through the files and add their Uri to the ArrayList for (int i = 0; i < files.length; i++) { File file = files[i]; Uri uri = Uri.fromFile(file); fileUris.add(uri); } Intent shareIntent = new Intent(Intent.ACTION_SEND); shareIntent.setType("*/*"); shareIntent.putExtra(Intent.EXTRA_STREAM,fileUris); shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); startActivity(Intent.createChooser(shareIntent, null));
Test文件夹内有2个文件,对应的URI为:
- file:///storage/emulated/0/Download/Stocks/g/test/test.txt
- file:///storage/emulated/0/Download/Stocks/g/test/stockdetails.xls
问题分析与修复方案
核心问题点
- 分享Intent类型错误:代码中创建了用于多文件分享的
ACTION_SEND_MULTIPLE但未使用,反而用了仅支持单文件的ACTION_SEND,导致Google Drive无法识别多文件数据。 - URI传递方式错误:
ACTION_SEND_MULTIPLE需要用putParcelableArrayListExtra传递URI列表,而非普通的putExtra。 - Android版本权限限制:
Uri.fromFile()生成的file://URI在Android 7.0+会触发权限限制,Google Drive无法直接访问该路径文件,而WhatsApp有特殊权限适配。
修复步骤
1. 注册FileProvider(AndroidManifest.xml)
在application标签内添加配置,生成安全的content://类型URI:
<application> ... <provider android:name="androidx.core.content.FileProvider" android:authorities="${applicationId}.fileprovider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider> ... </application>
2. 创建文件路径配置(res/xml/file_paths.xml)
指定FileProvider可访问的外部存储路径:
<?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="http://schemas.android.com/apk/res/android"> <external-path name="downloads" path="Download/" /> </paths>
3. 修改分享代码
File directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); // 用File构造方法拼接路径,避免字符串拼接的潜在错误 File folderData = new File(directory, "Stocks/Branch/Test"); // 检查文件夹有效性 File[] files = folderData.listFiles(); if (files == null || files.length == 0) { // 可添加提示:文件夹为空或不存在 return; } ArrayList<Uri> fileUris = new ArrayList<>(); // 获取应用专属的FileProvider权限标识 String authority = getApplicationContext().getPackageName() + ".fileprovider"; for (File file : files) { // 生成符合权限要求的content://类型URI Uri uri = FileProvider.getUriForFile(getApplicationContext(), authority, file); fileUris.add(uri); } // 使用正确的多文件分享Intent Intent shareIntent = new Intent(Intent.ACTION_SEND_MULTIPLE); shareIntent.setType("*/*"); // 正确传递多文件URI列表 shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, fileUris); // 授予目标应用文件读取权限 shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); startActivity(Intent.createChooser(shareIntent, "选择分享目标"));
内容的提问来源于stack exchange,提问作者Qfriend
相关产品推荐
相关产品推荐

