如何显示通过一个意图是与不同的应用程序兼容的图像意图、应用程序、图像、不同

2023-09-06 06:32:34 作者:云丢了天哭了。

我想要共享的图像我有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

选项A)使用Uri.fromFile

Uri agendaUri = Uri.fromFile(agendaFile); 
// the value -> file:///data/data/com.mypackage/files/agenda.jpg

结果

在Gmail中,是attatched到电子邮件中的图像? 否 TweetDeck的,是添加到的tweet消息的形象呢? 是

Results

Gmail, is the image attatched to the email? NO Tweetdeck, is the image added to the tweet message? YES

Uri agendaUri = Uri.parse(agendaFile.toURI().toString()); 
// the value -> file:/data/data/com.mypackage/files/agenda.jpg

结果

在Gmail中,是attatched到电子邮件中的图像? 是 TweetDeck的,是添加到的tweet消息的形象呢? 否

Results

Gmail, is the image attatched to the email? YES Tweetdeck, is the image added to the tweet message? NO

在这两种情况下我发的意图是这样的:

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"));