【问题标题】:How to attach a Bitmap when launching ACTION_SEND intent启动 ACTION_SEND 意图时如何附加位图
【发布时间】:2013-06-14 04:17:12
【问题描述】:

我有这个代码:

 Intent intent = new Intent(); 
 intent.setAction(Intent.ACTION_SEND); 
 startActivity(intent); 

这将在 Android 上成功启动消息应用程序。

但是在启动意图时如何附加位图对象?

我已阅读http://developer.android.com/reference/Android/content/Intent.html, 我需要的壁橱里的东西是EXTRA_STREAM,就像这样:

intent2.putExtra(Intent.EXTRA_STREAM, _uri);

但我的情况是,我有一个 Bitmap 对象的引用,而不是 Bitmap 的 URI。

请告诉我如何附加位图对象?

【问题讨论】:

  • 你要发送图片吗??
  • 保存该位图并提供 uri ....
  • @segi :: 是的,我想发送图片。
  • @Triode:我不知道 URI 因为我刚刚使用 dev cam 拍摄了图像

标签: android bitmap mms


【解决方案1】:
    String pathofBmp = Images.Media.insertImage(getContentResolver(), bitmap,"title", null);
    Uri bmpUri = Uri.parse(pathofBmp);
    final Intent emailIntent1 = new Intent(     android.content.Intent.ACTION_SEND);
    emailIntent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    emailIntent1.putExtra(Intent.EXTRA_STREAM, bmpUri);
    emailIntent1.setType("image/png");

其中 bitmap 是您必须存储在 SD 卡中的位图对象。然后将该 Uri 用于 shareimage。

【讨论】:

  • Images.Media.insertImage(getContentResolver(), bitmap,"title", null);返回空
  • @Riser 这完全符合预期,但因为我们将图像存储在 SD 卡中。是不是只是临时存储,因为如果不是,如果用户共享很多图像,这会淹没SD卡吗?我们应该有一些代码来删除这些。我猜这些是临时存储,因为我在我的画廊中看不到这些。确认一下?非常感谢您的回答
  • @Riser 我再次检查了那些永久存储在图片中的内容。这可以淹没他的 SD 卡。是我们可以指定图像名称的任何方式。所以我们可以用旧文件替换新文件。
  • 这也需要android.permission.WRITE_EXTERNAL_STORAGE,或grantUriPermission()
【解决方案2】:

您必须先将位图保存到文件中。您可以将其保存到应用程序的缓存中

private void shareBitmap (Bitmap bitmap,String fileName) {
    try {
        File file = new File(getContext().getCacheDir(), fileName + ".png");
        FileOutputStream fOut = new FileOutputStream(file);
        bitmap.compress(CompressFormat.PNG, 100, fOut);
        fOut.flush();
        fOut.close();
        file.setReadable(true, false);
        final Intent intent = new Intent(     android.content.Intent.ACTION_SEND);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
        intent.setType("image/png");
        startActivity(intent);
    } catch (Exception e) {
        e.printStackTrace();
    }

}

【讨论】:

  • 本例中最重要的:file.setReadable(true, false); 否则,其他App(你可能是startActivity的结果)将无法读取图像。所有其他答案都忽略了这一点..
  • 分享后如何删除??
【解决方案3】:

试试这个可能对你有帮助:

ByteArrayOutputStream bos = new ByteArrayOutputStream();  
yourbitmapimagename.compress(CompressFormat.PNG, 0, bos);
Intent intent = new Intent(); 
intent.setAction(Intent.ACTION_SEND); 
intent.setType("*/*"); 
intent.putExtra(Intent.EXTRA_STREAM, bos.toByteArray());
startActivity(intent); 

【讨论】:

  • 抛出 ClassCastException: "byte[] cannot be cast to android.os.Parcelable"
  • 我认为你必须将 java.io.ByteArrayOutputStream 导入你的类。
【解决方案4】:
 String cc=trlink.toString();
 Intent share = new Intent(Intent.ACTION_SEND);
 share.setType("text/plain");
 share.putExtra(Intent.EXTRA_TEXT,cc);
 startActivity(Intent.createChooser(share,"Share Text"));

【讨论】:

    最近更新 更多