0

A few years back I wrote an Android app targeting api lvl 14 and it worked without a problem since then. Now however with Android 11 file storage changes Im forced to make changes there. In the app the user can take some picture and then send all of them together in one email as multiple attachments. Since I don't need to access the images from outside the app I adjusted the "take photo" part to be the same as here: https://developer.android.com/training/camera/photobasics. I create the files like this:

File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
    imageFileName,  /* prefix */
    ".jpg",         /* suffix */
    storageDir      /* directory */
);

then I use a FileProvider to get the uri:

Uri photoURI = FileProvider.getUriForFile(mContext,
                        "com.my.app.package.fileprovider",
                        photoFile);
imageUri = photoURI.toString();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
((Activity) mContext).startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);

I declared the provider in the manifest:

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

and the file_paths xml looks like this:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-files-path name="my_images" path="Pictures" />
</paths>

This all works well. The problem starts when I try to add the files to the intent. Here is my code:

Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_EMAIL, new String[] { email });
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
LinkedList<Uri> uries = new LinkedList<Uri>();
if (imageFiles != null)
{
    for(Uri file:imageFiles){
        uries.add(file);
    }
}
intent.putExtra(Intent.EXTRA_STREAM, uries);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); //added this after reading some other posts but this changed nothing
startActivity(Intent.createChooser(intent, "Send email..."));

This worked when I used the Uri.fromFile method to get the uries. Now the imageFiles list contains the content Uris returned by file provider. I get the toast "could not attach file" and in the logs I see an error from outside my app that looks like this:

2021-08-10 18:30:36.481 20911-20911/? E/InputConnectionWrapper: InputConnectionWrapper.waitForInputConnectionFutureInternal():1486 Failed to get the input connection call's result.
    java.util.concurrent.TimeoutException: Waited 100 milliseconds (plus 112616 nanoseconds delay) for oww@bb95256[status=PENDING, info=[task=[running=[RUNNING ON ICWrapper-1], jhp@98272d7]]]
        at otn.get(PG:50)
        at jhz.u(PG:2)
        at jhz.t(PG:2)
        at jhy.e(PG:8)
        at jih.D(PG:1)
        at jih.x(PG:1)
        at jhz.f(PG:1)
        at com.android.inputmethod.latin.LatinIME.f(PG:118)
        at dro.onStartInputView(PG:28)
        at android.inputmethodservice.InputMethodService.startViews(InputMethodService.java:1955)
        at android.inputmethodservice.InputMethodService.showWindow(InputMethodService.java:1884)
        at android.inputmethodservice.InputMethodService$InputMethodImpl.showSoftInput(InputMethodService.java:638)
        at android.inputmethodservice.IInputMethodWrapper.executeMessage(IInputMethodWrapper.java:220)
        at com.android.internal.os.HandlerCaller$MyHandler.handleMessage(HandlerCaller.java:44)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7405)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:502)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:980)
2021-08-10 18:30:36.487 12556-12556/? E/Gmail: ComposeActivity:Error adding attachment
    ggw: SecurityException when openAssetFileDescriptor.
        at ggx.a(PG:5)
        at ggx.a(PG:45)
        at dob.a(PG:146)
        at dni.run(PG:2)
        at dob.a(PG:184)
        at dob.a(PG:153)
        at dob.a(PG:454)
        at djz.a(Unknown Source:3)
        at afec.a(Unknown Source:5)
        at ahej.a(PG:2)
        at ahel.run(PG:9)
        at ahgz.run(Unknown Source:7)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:462)
        at afcd.run(PG:2)
        at adoe.run(Unknown Source:3)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7405)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:502)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:980)

This happens on Android 10 but I presume on Android 11 it cannot work as well. I don't understand the error and I wasn't able to find any working solution that I can use myself. Im a bit lost here and would appreciate any help.

  • You did not google, did you? https://stackoverflow.com/questions/15577438/how-can-i-share-multiple-files-via-an-intent – blackapps Aug 10 '21 at 18:43
  • well, thanks for the comment I thought my solution was the same at first, but then I found that I use putExtra instead of putParcelableArrayListExtra, changing this one thing solve the problem for me. I thought the issue was with FileProvider and thats why I wasn't doing the right search. – user3092760 Aug 10 '21 at 20:00

1 Answers1

0

It worked after I changed intent.putExtra to intent.putParcelableArrayListExtra.