我想要共享的图像我有previously保存在磁盘上,发送 Intent.ACTION_SEND
。问题是,我无法找到一个方法来与不同的应用程序,官方Gmail应用和TweetDeck的在我的案件兼容。
I'm trying to share an image I have previously saved on disk, sending an Intent.ACTION_SEND
. The problem is that I can't find a way to be compatible with different apps, official Gmail app and TweetDeck in my case.
我想分享的图像包含在文件
:
The image I want to share is contained in a File
:
File agendaFile;
// its path using getAbsolutePath() -> /data/data/com.mypackage/files/agenda.jpg
Uri agendaUri = Uri.fromFile(agendaFile);
// the value -> file:///data/data/com.mypackage/files/agenda.jpg
Uri agendaUri = Uri.parse(agendaFile.toURI().toString());
// the value -> file:/data/data/com.mypackage/files/agenda.jpg
在这两种情况下我发的意图是这样的:
In both cases I send the intent like this:
final Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setType("image/jpg");
intent.putExtra(android.content.Intent.EXTRA_STREAM, agendaUri);
startActivity(Intent.createChooser(intent, "title"));
那么,有没有任何其他选择共享的图像?它是如何共享的图像是与大多数应用程序尽可能兼容的最佳方法是什么?
So, is there any other options to share an image? How is it the best way to share an image being compatible with most apps as possible?
谢谢!
我终于解决了这个问题,存储图像的MediaStore。而不是使用文件的URI是我做的是:
I finally solved the problem storing the image at the MediaStore. Instead of using the URI of the File what I do is:
String agendaFilename = agendaFile.getAbsolutePath();
final ContentValues values = new ContentValues(2);
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
values.put(MediaStore.Images.Media.DATA, agendaFilename);
final Uri contentUriFile = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
最后,我用 contentUriFile
:
final Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setType("image/jpg");
intent.putExtra(android.content.Intent.EXTRA_STREAM, contentUriFile);
startActivity(Intent.createChooser(intent, "title"));