Android Attaching a file to GMAIL - Can't attach empty file
Asked Answered
K

5

21

I had a program that would always attach the same file to GMAIL (Compose > Attach File > Open From > "MyProgram"). It would always select the same file.

What it was doing was:

String path = Environment.getExternalStorageDirectory() + "/file.3gp";
File f = new File(path);
Uri data = Uri.fromFile(f);
Intent i = new Intent();
i.setData(data);
setResult(Activity.RESULT_OK, i);
finish();

This was working fine until Android 6.0. Now, I receive the following error when trying to use it:

Can't attach empty file

Astro File Sharing is giving me the same error (can be an old build).

However, I installed ES File Explorer, and when I do the same routine, and select the file, I receive a Dialog which says:

Pick up file as

  • Normal Android Way (For MMS,Gmail,...)
  • File Way (Try this if above fails)

The "File Way" will fail as my program does. The "Normal Android Way" will work fine.

Does anyone have any idea on what it does, so I can replicate?

Thanks in advance!

OBS: Already tried the putExtra(STREAM, path) a lot of times, but without success.

Kucera answered 31/8, 2015 at 19:37 Comment(5)
How long is the name of the file?Raper
@Skizo this is the full path + name: "file:///sdcard/Example_1MBattachment.mpx"Kucera
Try to put a name with less letters, I know one guy that it was his bad.Raper
@Skizo Tried with test.mpx and test2.3gp. None worked. They do work with ES File Explorer.Kucera
I have tried changing the file path from file:///... to content://... and now the toast message is different. Now it is saying "Couldn't attach file.". I am still using the setData, and putExtra is not working (does nothing)Kucera
K
19

Ok, got it to work now, after a lot of research and intercepting some Intents.

What I had to do was change the file:/// to content://.

I did this following this information from Android: https://developer.android.com/reference/android/support/v4/content/FileProvider.html

The only major change was that I used a hard-coded path to /sdcard/file.ext. Also, the line

getUriForFile(getContext(), "com.mydomain.fileprovider", newFile);

was changed to

Uri contentUri = FileProvider.getUriForFile(this, "com.mydomain.fileprovider", newFile);

Also had to include:

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
i.setData(contentUri);

I do not really understand why I had to change from File to Content, but after this, the file is now being attached again! See the link if you face this issue, and don't forget about the new .xml that needs to be created.

Kucera answered 1/9, 2015 at 17:25 Comment(4)
can you please elaborate how you get it working? I'm in the same pit. I want to know in details. Thanks.Plaster
@Plaster Hey, I am sorry but I really can't remember exactly how I got to the bottom of my issue (it has been quite a while). I do know that I had a lot of trial and errors. I will take a lookt at it tomorrow once I get to work. Try looking into the link that I reference, that might help you, specially the first two paragraphs explaining about Content vs FileKucera
Great! Did it had anything to do with the way described above? Or was it totally different?Kucera
@Plaster the solution has been mentioned here: https://mcmap.net/q/447771/-android-6-cannot-share-files-anymoreRetortion
T
18

See the following question:

android-6-cannot-share-files-anymore

This behavior is with Android 6.0 and Gmail. Please see the following thread.

Issue 3141 - android-developer-preview

If you go to Settings->Apps->Gmail->Permissions and enable the "Storage" permission manually, then the share works.

Gmail should ask for "Storage" permission in this scenario and it would work as it did in all the past version of Android.

Other email apps should handle attachments correctly.

Thenna answered 23/10, 2015 at 22:9 Comment(3)
Thanks mkabatek. I am not sure if I can test it again, since it was already changed, but if I can I will report the findings!Kucera
I had the same issue on nexus 6p and could fix it with this settings. On Nexus 7 with Android 6.0.1 this was the default setting and I didn'n have to do it manually.Upbeat
it doesn't work. I have added this permission to gmail but I doesn't work.Cyanocobalamin
P
2

Here is how to fix it.

Go to Settings -> Apps -> Gmail -> Permissions Turn on the permission for "Storage" That work-around solved the issue for me.

Plumley answered 12/2, 2016 at 3:58 Comment(0)
F
0

I couldn't find a clear answer(sending an attachment on gmail without SD card) I tried to copy to another file name but no cigar. The way I got it to work was copy to the Downloads folder and go from there.

Get the path for Downloads with this Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)

pulls the full path to downlaods folder copy the attachemnt to there and we are good. Files over 20M don't go

Fingernail answered 8/9, 2017 at 19:40 Comment(0)
A
0

Share any file using INTENT provided given file path

    //File file= shareable File Path
    Uri uri = FileProvider.getUriForFile(this, "com.example.myapp.fileprovider", file); 
    //FileProvider authorities from Manifest
            
    Intent sharingIntent = new Intent(Intent.ACTION_SEND);
    sharingIntent.setType("*/*");

    sharingIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);
    startActivity(sharingIntent);

Just match your authorities from the manifest in arguments of method getUriForFile.

In Manifest

 <provider
        android:name="androidx.core.content.FileProvider"
        android:authorities="com.example.myapp.fileprovider"
        android:grantUriPermissions="true"
        android:exported="false">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/filepaths" />
    </provider>
Afterglow answered 5/6, 2020 at 16:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.