I’m having trouble with my Android app that handles file attachments for Gmail. The app used to work perfectly before Android 6.0, but now it’s throwing an error.
What my code does:
String filePath = Environment.getExternalStorageDirectory() + "/media.mp4";
File mediaFile = new File(filePath);
Uri fileUri = Uri.fromFile(mediaFile);
Intent result = new Intent();
result.setData(fileUri);
setResult(Activity.RESULT_OK, result);
finish();
The problem: When Gmail tries to attach the file, I get this message: Can’t attach empty file
I noticed that ES File Explorer has two options when sharing files:
- Normal Android Way (works with Gmail)
- File Way (fails like my app)
What’s the difference between these approaches? How can I implement the “Normal Android Way” in my code?
I’ve tried using putExtra(EXTRA_STREAM, filePath) multiple times but it doesn’t work. Any ideas on how to fix this attachment issue?