如何通过与不同应用程序兼容的意图显示图像 [英] How to show an image through an intent being compatible with different apps

查看:26
本文介绍了如何通过与不同应用程序兼容的意图显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试共享我之前保存在磁盘上的图像,发送 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,图片是否附在电子邮件中?
  • Tweetdeck,图片是否添加到推文消息中?
  • Uri agendaUri = Uri.parse(agendaFile.toURI().toString()); 
    // the value -> file:/data/data/com.mypackage/files/agenda.jpg
    

    结果

    • Gmail,图片是否附在电子邮件中?
    • Tweetdeck,图片是否添加到推文消息中?
    • 在这两种情况下,我都这样发送意图:

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

      这篇关于如何通过与不同应用程序兼容的意图显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆