【问题标题】:Share text and image at the same time in Android在 Android 中同时分享文字和图片
【发布时间】:2018-02-11 21:33:39
【问题描述】:

我已经尝试了几个小时与Intent.ACTION_SEND 共享文本和图像(同时)。尽管我尽一切努力让它发挥作用,但我仍然无法做到。

我在 Google 上搜索过,这可能很奇怪,但我只找到了 2 篇关于如何同时分享文本和图像的帖子,我试过了,但没有一个他们工作了。我尝试使用一种方法,结果如下:

Uri imageToShare = Uri.parse("android.resource://com.example.application/drawable/invite"); //Image to be shared
String textToShare = "Text to be shared"; //Text to be shared

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_SUBJECT, textToShare);
shareIntent.setType("*/*");
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shareIntent.putExtra(Intent.EXTRA_TEXT, imageToShare);
startActivity(Intent.createChooser(shareIntent, "Share with"));
  • 上面的代码显示错误消息“共享失败,请重试。”
  • 我要发送的图像是invite.png,它位于drawable 文件夹中。
  • 我使用的是 Android O (8.0) 手机。

我希望以上信息对您有用。我仍然是 Java 的初学者,任何帮助将不胜感激!

【问题讨论】:

    标签: java android image text share


    【解决方案1】:

    首先,任何应用都不需要支持通过ACTION_SEND 共享文本和图像。

    其次,图片的Uri进入EXTRA_STREAMas notyou pointed out

    第三,很少有应用会知道如何将android.resource 处理为Uri 方案。毕竟,那不是the documented scheme for an EXTRA_STREAM Uri。它必须是content,您通过ContentProvider 提供您的内容,例如FileProvider

    第四,如果你指定实际的 MIME 类型,而不是使用通配符,你可能会有更好的运气。

    【讨论】:

    • 感谢您的回答。你能帮第三个吗?图片在drawable文件夹中。
    • @TouficBatache:我不清楚用户为什么要共享固定图像。无论如何,您将需要使用一些ContentProvider。例如,您可以将该图像复制到设备文件系统上的一个文件中(例如,在getCacheDir() 中)并使用FileProvider 提供它。
    • 你认为我可以用 base64 格式的图像吗?该图片是该应用的宣传图片;)
    • @TouficBatache:“你认为我可以使用 base64 格式的图像吗?” -- 我怀疑许多其他应用程序会知道如何处理 base64 图像。
    • 我的意思是我有一个脚本可以将其转换为位图 uri……这可能吗?
    【解决方案2】:

    我终于设法以更简单的方式做到了!我从图像中创建了一个base64 并将其作为字符串添加到strings.xml 文件中(确保从字符串的开头删除data:image/png;base64,)。

    我使用的代码如下:

    byte[] decodedString = Base64.decode(getString(R.string.share_image), Base64.DEFAULT); //The string is named share_image.
    Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
    Uri imageToShare = Uri.parse(MediaStore.Images.Media.insertImage(MainActivity.this.getContentResolver(), decodedByte, "Share image", null)); //MainActivity.this is the context in my app.
    String textToShare = "Sample text"; //Text to be shared
    
    Intent share = new Intent(Intent.ACTION_SEND);
    share.setType("image/*");
    share.putExtra(Intent.EXTRA_TEXT, textToShare);
    share.putExtra(Intent.EXTRA_STREAM, imageToShare);
    startActivity(Intent.createChooser(share, "Share with"));
    

    我希望这个答案能解决很多人的问题!

    非常感谢@notyou 和@CommonsWare

    【讨论】:

      【解决方案3】:

      此代码将允许您发送任何带有文本的位图图像。

      Bitmap decodedByte = BitmapFactory.decodeResource(getResources(), R.drawable.refer_app);
                  Uri imageToShare = Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(), decodedByte, "Share app", null));   // in case of fragment use [context].getContentResolver()
                  String shareMessage = "message to share";
                  Intent share = new Intent(Intent.ACTION_SEND);
                  share.setType("image/*");
                  share.putExtra(Intent.EXTRA_TEXT, shareMessage);
                  share.putExtra(Intent.EXTRA_STREAM, imageToShare);
                  startActivity(Intent.createChooser(share, "Share via"));
      

      【讨论】:

      • 这对我有用,但有没有办法防止我试图分享的照片被保存在我的手机中?就目前而言,每当我点击分享时,照片的副本都会保存在本地。
      【解决方案4】:

      这对我有用:

      Intent iShare = new Intent(Intent.ACTION_SEND);
      iShare.setType("image/*");
      iShare.putExtra(Intent.EXTRA_TEXT, "Your text”);
      iShare.putExtra(Intent.EXTRA_STREAM, Your image);
      startActivity(Intent.createChooser(iShare, "Choose app to share photo with!"));
      

      【讨论】:

      • 感谢您的回答。 Your image 是 Uri 吗?
      • 是的,它正在使用 Uri.fromFile
      • 好的,我用 WhatsApp 尝试了你的代码,它发送了一个空白的 Untitled 文档。
      • 嗯,它对我有用(但它来自相机拍摄的照片,如果这有什么不同的话)。
      • 不确定...我会尝试使用另一张图片并让您随时更新。
      猜你喜欢
      • 2023-04-06
      • 2021-12-25
      • 1970-01-01
      • 2014-03-09
      • 1970-01-01
      • 2013-09-08
      • 1970-01-01
      • 2013-03-07
      相关资源
      最近更新 更多