Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ACTION_SEND - Share File with Different Filename Than That on Disc

Short Version of Question..

Is it possible to launch a ACTION_SEND intent in order to share a file on disc but to have a different filename used by the share than the filename on disc?

Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/png");
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile("somepath/3c72ae1adacb.dat"));
// BEGIN Hopefully something like this exists....
shareIntent.putExtra(Intent.EXTRA_FILENAME, "CuteKittens.png"));
// END
startActivity(shareIntent);

Long Version...

For security reasons some of the files used within my Android app have obscured filenames on disc such as "3c72ae1adacb.dat".

I would like for logged in users to be able to share these files but with their un-obscured filename such as "CuteKittens.png".

Is this achievable? (Ideally without copying the file on disc, sharing the copy and finally deleting it which seems like a lot of work for something so straight forward).

like image 323
Oliver Pearmain Avatar asked Apr 22 '14 14:04

Oliver Pearmain


1 Answers

You should not be using file:// Uri values in general, as there is no guarantee that other apps can read those files.

You are welcome to implement a ContentProvider that serves up these files, where your Uri values have the "un-obscured filename". So, rather than:

shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile("somepath/3c72ae1adacb.dat"));

you would have:

shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("content://your.authority.goes.here/files/CuteKittens.png"));

and the ContentProvider would know, by one means or another, that the content for CuteKittens.png is found in somepath/3c72ae1adacb.dat.

The built-in FileProvider is almost what you need, except that I assume that the filenames (both original and renamed) are not known at compile time.

like image 123
CommonsWare Avatar answered Nov 02 '22 14:11

CommonsWare