【问题标题】:Android ACTION_SEND (MULTIPLE) intent does not send files to Google DriveAndroid ACTION_SEND (MULTIPLE) Intent 不会将文件发送到 Google Drive
【发布时间】:2020-10-15 12:43:50
【问题描述】:

我将图片保存为文件。然后我尝试分享它们。

它适用于所有 Messenger 应用,但不适用于 Google 云端硬盘。我收到来自 Google 云端硬盘的通知错误。

首先,在分享弹出窗口中选择谷歌驱动器后,我可以choose Folder。这意味着 Google 云端硬盘会看到我的文件(或者可能只是名称?)。

之后,我立即从 Google 驱动器收到 error notification,说“上传错误,无法计划上传 2 个文件”(或我选择的其他文件数)。

意图创建方法:

@NotNull
private Intent initIntent() {
    Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
    intent.setType("image/jpg");
    intent.putExtra(Intent.EXTRA_SUBJECT, "Pictures");
    intent.putExtra(Intent.EXTRA_TEXT, "Pictures share");
    return intent;
}

尝试发送一个(使用 ACTION_SEND_MULTIPLE 或使用 ACTION_SEND),尝试添加不同的 EXTRA_SUBJECT、EXTRA_TEXT、EXTRA_MAIL,添加 EXTRA_TEXT 作为 ArrayList(使用 intent.putCharSequenceArrayListExtra)。尝试将不同的标志添加到意图作为 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)。没有任何帮助。

intent 方法的额外图片:

private void addPicturesExtra(Intent intent) {
    ArrayList<Uri> uris = new ArrayList<>(pictures.size());
    for (PicturePresentation picture : pictures) {
        File picFile = new File(picture.getPath());
        Uri uri = Uri.fromFile(picFile);
        uris.add(uri);
    }
    intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
}

正如我之前所说,尝试使用一个 Uri(图片文件路径)并使用 ArrayList 作为 Intent extra 的参数和一个 Uri 对象。

意图启动方法:

private void startIntent(Intent intent) {
    try {
        activity.startActivity(Intent.createChooser(intent, "Send"));
    } catch (Exception ex) {
        ExceptionHandler.handle(ex);
    }
}

尝试使用或不使用选择器启动它。它没有帮助。很奇怪,它适用于所有其他应用,但不适用于 Google 驱动器。

您可以在下面找到 Logcat 尝试将图片上传到 Google 驱动器时的堆栈跟踪。

2020-10-15 15:47:49.871  V/FA: Activity resumed, time: 1113377792
2020-10-15 15:47:49.890  V/FA: Connecting to remote service
2020-10-15 15:47:49.899  D/FA: Connected to remote service
2020-10-15 15:47:49.900  V/FA: Processing queued up service tasks: 1
2020-10-15 15:47:54.904  V/FA: Inactivity, disconnecting from the service
2020-10-15 15:47:54.905  W/ConnectionTracker: Exception thrown while unbinding
    java.lang.IllegalArgumentException: Service not registered: com.google.android.gms.measurement.internal.zzji@6e14ca2
        at android.app.LoadedApk.forgetServiceDispatcher(LoadedApk.java:1759)
        at android.app.ContextImpl.unbindService(ContextImpl.java:1786)
        at android.content.ContextWrapper.unbindService(ContextWrapper.java:751)
        at com.google.android.gms.common.stats.ConnectionTracker.zza(com.google.android.gms:play-services-basement@@17.3.0:55)
        at com.google.android.gms.common.stats.ConnectionTracker.unbindService(com.google.android.gms:play-services-basement@@17.3.0:50)
        at com.google.android.gms.measurement.internal.zzio.zzag(com.google.android.gms:play-services-measurement-impl@@17.6.0:245)
        at com.google.android.gms.measurement.internal.zzio.zzal(com.google.android.gms:play-services-measurement-impl@@17.6.0:262)
        at com.google.android.gms.measurement.internal.zzio.zzc(com.google.android.gms:play-services-measurement-impl@@17.6.0:336)
        at com.google.android.gms.measurement.internal.zzir.zza(com.google.android.gms:play-services-measurement-impl@@17.6.0:2)
        at com.google.android.gms.measurement.internal.zzai.run(com.google.android.gms:play-services-measurement-impl@@17.6.0:7)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:462)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at com.google.android.gms.measurement.internal.zzfs.run(com.google.android.gms:play-services-measurement-impl@@17.6.0:21)

我唯一的例外是 IllegalArgumentException,我认为它来自 GoogleAnalytics。无论如何,我认为这与我面临的问题无关。 (我在其他一些情况下也有这种情况

我是否遗漏了一些明显的东西,还是只是 Google 驱动器的错误?任何形式的帮助都将不胜感激!

【问题讨论】:

  • 您没有将文件添加到您的意图中。
  • “我是否遗漏了一些明显的东西”——是的。自 Android 7.0 起,file Uri 值已被禁止。你应该会因为a FileUriExposedException 而崩溃。摆脱Uri.fromFile() 并使用FileProvider
  • @CommonsWare,您知道 ACTION_VIEW 会抛出 FileUriExposedException 而 ACTION_SEND 不会?只有在较新的 Android 版本上 ACTION_SEND 也会抛出它。后期更改/适应。
  • “您知道 ACTION_VIEW 会抛出 FileUriExposedException 而 ACTION_SEND 不会?” ——啊,这很有趣。我不知道他们没有检查额外的东西。谢谢!

标签: android android-intent share


【解决方案1】:

感谢 CommonsWare,我解决了这个问题!

所以,首先,我们需要使用

Uri uri = FileProvider.getUriForFile(context, "com.test", picFile)

而不是

Uri uri = Uri.fromFile(picFile);

所以 FileProvider 会为我们的文件生成特殊的 Uri,路径前带有 "content://"

然后我们需要在 App 清单中声明我们的 FileProvider。

    <provider
        android:name="androidx.core.content.FileProvider"
        android:authorities="com.test.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths" />
    </provider>

以及带有路径的附加文件

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-files-path
        name="pics"
        path="pics/" />
    <external-files-path
        name="pdf"
        path="pdf/" />
</paths>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 2020-03-10
    相关资源
    最近更新 更多