如何在启动ACTION_SEND意图时附加位图

KCR*_*aju 18 android mms bitmap

我有这个代码:

 Intent intent = new Intent(); 
 intent.setAction(Intent.ACTION_SEND); 
 startActivity(intent); 
Run Code Online (Sandbox Code Playgroud)

哪个将成功在Android上启动消息应用程序.

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

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

intent2.putExtra(Intent.EXTRA_STREAM,_uri);

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

请告诉我如何附加Bitmap对象?

Sag*_*yad 26

    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");
Run Code Online (Sandbox Code Playgroud)

位图是你的位图对象,必须存储在SD卡中.然后使用该Uri进行共享图像.

  • Images.Media.insertImage(getContentResolver(),bitmap,"title",null); 返回null (3认同)
  • @Riser我再次检查那些永久存储在图片中.这可以淹没他的SD卡.他们是我们可以指定图像名称的任何方式.所以我们可以用旧文件替换新文件. (2认同)
  • 这也需要`android.permission.WRITE_EXTERNAL_STORAGE`或`grantUriPermission()` (2认同)

Gil*_* SH 22

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

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();
    }

}
Run Code Online (Sandbox Code Playgroud)