Android Send Intent for file puts data uri into gm

2019-07-23 18:49发布

I need an Intent to send a file to apps such as messengers (WhatsApp, etc.) and Email clients (Gmail, etc.) at the same time. Gmail however for some reason I don't understand shows the file uri in the recipient field.

My original uri is: [package_name].debug.generic.tools.GenericFileProvider/external_files/1544617983061.pdf

The recipient end up as: //[package_name].debug.generic.tools.GenericFileProvider/external_files/1544617983061.pdf

Screenshot from Gmail:

enter image description here

Code with some bug:

Uri uri = FileProvider.getUriForFile(fragment.getActivity(), fragment.getActivity().getApplicationContext().getPackageName() + ".generic.tools.GenericFileProvider", externalFile);
Intent intentSend = new Intent(Intent.ACTION_SEND);
intentSend.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intentSend.setDataAndType(uri, "application/pdf");
intentSend.putExtra(Intent.EXTRA_STREAM, uri);
intentSend.putExtra(Intent.EXTRA_SUBJECT, name);
intentSend.putExtra(Intent.EXTRA_EMAIL, new String[]{"test@provider.com"});

try {
    fragment.startActivity(Intent.createChooser(intentSend, fragment.getString(R.string.external_file_send_chooser)));
} catch (android.content.ActivityNotFoundException ex) {
    Toast.makeText(fragment.getActivity(), R.string.external_file_send_no_activity, Toast.LENGTH_LONG).show();
}

Does anyone understand what is going on? How come my data uri is interpreted as recipient?

The file itself is attached correctly to the Email and WhatsApp also successfully sends the file.

EDIT: Question has already been answered here: Content URI passed in EXTRA_STREAM appears to "To:" email field

0条回答
登录 后发表回答