0

I have pictures saved as files. Then I try to share them.

It works well on all messenger apps but doesn't work with Google drive. I have a notification error from Google drive.

First of all, after choosing Google drive in the share popup I can choose Folder. That means that Google drive see my files (or maybe only names?).

After that I immediately have error notification from Google drive, saying "Upload error, Cannot plan upload of 2 files" (or another count of files I've chosen).

Intent creation method:

@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;
}

Tried sending one (with ACTION_SEND_MULTIPLE or with ACTION_SEND), tried adding different EXTRA_SUBJECT, EXTRA_TEXT, EXTRA_MAIL, adding EXTRA_TEXT as ArrayList (with intent.putCharSequenceArrayListExtra). Tried adding different flags to intent as intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK). Nothing helped.

Pictures extra for intent method:

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);
}

As I said before, tried with one Uri (picture file path) and with both ArrayList as an argument for intent extra and one Uri object.

Intent start method:

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

Tried starting it with or without chooser. It didn't help. Very strange that it works with all other apps but not with Google drive.

Below you can find the stack trace of Logcat when try to upload pictures to Google drive.

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)

The only exception I have there is IllegalArgumentException which I think comes from GoogleAnalytics. Anyway, I think it has nothing with the problem I'm facing. (I have it in some other situations too)

Am I missing something obvious or is it just Google drive's bug? Would appreciate any kind of help!

  • You are not adding files to your intent. – blackapps Oct 15 '20 at 12:49
  • 1
    "Am I missing something obvious" -- yes. `file` `Uri` values have been banned since Android 7.0. You should be crashing with [a `FileUriExposedException`](https://stackoverflow.com/q/38200282/115145). Get rid of `Uri.fromFile()` and use `FileProvider`. – CommonsWare Oct 15 '20 at 12:51
  • 2
    @CommonsWare, you are aware that ACTION_VIEW would throw a FileUriExposedException where ACTION_SEND would not? Only on the newer Android versions ACTION_SEND throws it too. Late change/adaptation. – blackapps Oct 15 '20 at 13:03
  • 1
    "you are aware that ACTION_VIEW would throw a FileUriExposedException where ACTION_SEND would not?" -- ah, that's interesting. I was not aware that they failed to check extras. Thanks! – CommonsWare Oct 15 '20 at 13:13

1 Answers1

1

Thanks to CommonsWare, I fixed that!

So, first of all, we need to use

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

instead of

Uri uri = Uri.fromFile(picFile);

So FileProvider generates special Uri for our file with a "content://" in front of path.

Then we need to declare our FileProvider in the App manifest.

    <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>

And additional file with pathes

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