Reputation: 256
I'm trying to create a file picker to connect with a cloud storage service. I have no problems attaching files to other applications, and even Gmail successfully gets back my Activity result and shows a proper thumbnail based on the URI / clipdata that I set.
The problem is that when you actually go to send the email, Gmail notifies the user "Couldn't send attachment" and sends the email without the attachment.
I don't know what I'm doing wrong. I have tried setting URI as Intent.setData() as well as using the URI and setting the Intent.setClipData(). In addition I tried explicitly including the flag for FLAG_GRANT_READ_URI_PERMISSION. None of these things solve the issue.
Anyone who has any idea what may be going wrong, please chime in. Other cloud providers seem to have no problem when attaching their content through this flow, so I just don't know what I'm doing wrong.
My code roughly looks as follows right now, and still won't work with Gmail:
Intent result = new Intent();
result.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
result.setData(uri);
if (getActivity().getIntent().getBooleanExtra(Intent.EXTRA_ALLOW_MULTIPLE, false)) {
String mime = Utils.getMimeType(document);
result.setClipData(new ClipData(document.getSimpleName(), new String[]{mime}, new ClipData.Item(uri)));
result.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
result.putExtra(Intent.EXTRA_STREAM, uri);
}
getActivity().setResult(Activity.RESULT_OK, result);
getActivity().finish();
Upvotes: 0
Views: 498
Reputation: 256
Turns out the problem was that the file-uri was located in the storage directory /storage/emulated/0/folder_name (new File(Environment.getExternalStorageDirectory(), "folder_name") did not work). By switching to Context.getExternalCacheDir() (/sdcard/Android/data/packageName/) Gmail was successfully able to retrieve the attachment.
Apparently Gmail has stricter permissions on accessing the files it is given access to than other applications (where it worked fine before the change). Just unfortunate there was no error message but rather a after-the-fact failure in Gmail for this situation, was not straightforward to discover why it did not work.
Upvotes: 0