ACTION_SEND - Share File with Different Filename Than That on Disc

心不动则不痛 提交于 2019-12-08 18:21:33

问题


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).


回答1:


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.



来源:https://stackoverflow.com/questions/23222702/action-send-share-file-with-different-filename-than-that-on-disc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!