Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 227
GuiFGDeo Avatar asked Aug 31 '15 19:08

GuiFGDeo


People also ask

Why can't I attach a file in Gmail Android?

Gmail needs storage permissions to access and attach files from your mobile device. Step 1: Open Settings > Apps > Gmail > Permissions > Storage. Make sure it is set to Allow so Gmail can access files on your phone and attach it.

Why wont it let me attach a file on Gmail?

If you are unable to attach files to Gmail message and you're seeing error messages while uploading attachments, make sure you have Flash installed. Visit Adobe Flash Player to check for updates and download the latest version. In addition, please make sure that Flash is not being blocked on your computer.

Why is my email not letting me attach a file?

The most common reason that an attachment won't send is that it is too big. These limits are set by whoever you use for email, whether it's an email account through your ISP or through an online provider like Yahoo or GMail. You should check with your email service provider to see what the limits are for attachments.


1 Answers

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.

like image 105
GuiFGDeo Avatar answered Dec 10 '22 06:12

GuiFGDeo