Android: Share Simple Text As File/attachment
I have an app that collects some info and allows the user to share it using Android's Intent framework. So far, it shares the report as plain text: using putExtra(Intent.EXTRA_TEXT
Solution 1:
Use FileProvider
to share a file that is not publicly accessible. Check out Setting Up File Sharing for a short tutorial.
Sending text as an email attachment is a bit tricky though. You can't use text/plain
as type because that would imply the data is supplied in EXTRA_TEXT
. But text in this extra will end up in the email body, not in an attachment.
What should work is the following:
UrifileUri= FileProvider.getUriForFile(context, "org.example.provider.authority", reportFile);
IntentshareIntent=newIntent(Intent.ACTION_SEND);
shareIntent.setType("*/*");
shareIntent.putExtra(Intent.EXTRA_STREAM, fileUri);
startActivity(Intent.createChooser(shareIntent, null));
You should also make sure that FileProvider.getType()
returns text/plain
for the report file. The easiest way to accomplish this is by using .txt
as file extension and FileProvider
will automatically do the right thing.
Post a Comment for "Android: Share Simple Text As File/attachment"