60

我想在 android 中使用 ACTION_SEND 一起共享文本 + 图像,我使用下面的代码,我只能共享图像但我不能与它共享文本,

private Uri imageUri;
private Intent intent;

imageUri = Uri.parse("android.resource://" + getPackageName()+ "/drawable/" + "ic_launcher");
intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, "Hello");
intent.putExtra(Intent.EXTRA_STREAM, imageUri);
intent.setType("image/*");
startActivity(intent);

对此有什么帮助吗?

4

13 回答 13

66

您可以通过这些代码共享纯文本

String shareBody = "Here is the share content body";
    Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
        sharingIntent.setType("text/plain");
        sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
        sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
        startActivity(Intent.createChooser(sharingIntent, getResources().getString(R.string.share_using)));

所以你的完整代码(你的图像+文本)变成

      private Uri imageUri;
      private Intent intent;

            imageUri = Uri.parse("android.resource://" + getPackageName()
                    + "/drawable/" + "ic_launcher");

            intent = new Intent(Intent.ACTION_SEND);
//text
            intent.putExtra(Intent.EXTRA_TEXT, "Hello");
//image
            intent.putExtra(Intent.EXTRA_STREAM, imageUri);
//type of things
            intent.setType("*/*");
//sending
            startActivity(intent);

我刚刚替换image/**/*

更新

Uri imageUri = Uri.parse("android.resource://" + getPackageName()
        + "/drawable/" + "ic_launcher");
 Intent shareIntent = new Intent();
 shareIntent.setAction(Intent.ACTION_SEND);
 shareIntent.putExtra(Intent.EXTRA_TEXT, "Hello");
 shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
 shareIntent.setType("image/jpeg");
 shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
 startActivity(Intent.createChooser(shareIntent, "send"));
于 2013-12-02T16:41:09.790 回答
17

请查看此代码,我可以一起共享文本和图像

Intent shareIntent;
    Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher);
    String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+"/Share.png";
    OutputStream out = null;
    File file=new File(path);
    try {
        out = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
        out.flush();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    path=file.getPath();
    Uri bmpUri = Uri.parse("file://"+path);
    shareIntent = new Intent(android.content.Intent.ACTION_SEND);
    shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
    shareIntent.putExtra(Intent.EXTRA_TEXT,"Hey please check this application " + "https://play.google.com/store/apps/details?id=" +getPackageName());
    shareIntent.setType("image/png");
    startActivity(Intent.createChooser(shareIntent,"Share with"));

不要忘记给予 WRITE_EXTERNAL_STORAGE 权限

同样在 facebook 中,它只能共享图像,因为 facebook 不允许通过意图共享文本

于 2017-05-29T08:05:17.900 回答
6

这可能是因为共享应用程序(FB、Twitter 等)可能没有读取图像的权限。

谷歌的文件说:

接收应用程序需要访问 Uri 指向的数据的权限。推荐的方法是:

http://developer.android.com/training/sharing/send.html

我不确定共享应用程序是否有权读取我们应用程序包中的图像。但是我的文件保存在

 Activity.getFilesDir()

无法读取。正如上面链接中的建议,我们可以考虑将图像存储在 MediaStore 中,共享应用程序具有读取权限。

Update1: ​​以下是在 Android 4.4 中与文本共享图像的工作代码。

        Bitmap bm = BitmapFactory.decodeFile(file.getPath());
        intent.putExtra(Intent.EXTRA_TEXT, Constant.SHARE_MESSAGE
            + Constant.SHARE_URL);
        String url= MediaStore.Images.Media.insertImage(this.getContentResolver(), bm, "title", "description");
        intent.putExtra(Intent.EXTRA_STREAM, Uri.parse(url));
        intent.setType("image/*");
        startActivity(Intent.createChooser(intent, "Share Image"));

副作用是我们在 MediaStore 中添加了一个图像。

于 2015-07-12T04:34:57.030 回答
4

尝试使用此代码。我正在从 drawable 上传 ic_launcher。您可以使用图库或位图中的文件来更改它。

void share() {
    Bitmap icon = BitmapFactory.decodeResource(getResources(),
                R.drawable.ic_launcher);
    Intent share = new Intent(Intent.ACTION_SEND);
    share.setType("image/jpeg");
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    File f = new File(Environment.getExternalStorageDirectory()
                + File.separator + "temporary_file.jpg");
    try {
        f.createNewFile();
        FileOutputStream fo = new FileOutputStream(f);
        fo.write(bytes.toByteArray());
    } catch (IOException e) {
        e.printStackTrace();
    }
    share.putExtra(Intent.EXTRA_TEXT, "hello #test");

    share.putExtra(Intent.EXTRA_STREAM,
            Uri.parse("file:///sdcard/temporary_file.jpg"));            
    startActivity(Intent.createChooser(share, "Share Image"));
}
于 2015-07-17T06:59:42.183 回答
4
 String bitmapPath = MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "title", null);
        Uri bitmapUri = Uri.parse(bitmapPath);

        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("image/png");
        intent.putExtra(Intent.EXTRA_STREAM, bitmapUri);
        intent.putExtra(Intent.EXTRA_TEXT, "This is a playstore link to download.. " + "https://play.google.com/store/apps/details?id=" + getPackageName());

        startActivity(Intent.createChooser(intent, "Share"));
于 2019-09-18T11:42:27.643 回答
4

我一直在寻找这个问题的解决方案,发现这个问题正在运行,希望对您有所帮助。

private BitmapDrawable bitmapDrawable;
private Bitmap bitmap1;
//write this code in your share button or function

bitmapDrawable = (BitmapDrawable) mImageView.getDrawable();// get the from imageview or use your drawable from drawable folder
bitmap1 = bitmapDrawable.getBitmap();
String imgBitmapPath= MediaStore.Images.Media.insertImage(getContentResolver(),bitmap1,"title",null);
Uri imgBitmapUri=Uri.parse(imgBitmapPath);
String shareText="Share image and text";
Intent shareIntent=new Intent(Intent.ACTION_SEND);
shareIntent.setType("*/*");
shareIntent.putExtra(Intent.EXTRA_STREAM,imgBitmapUri);
shareIntent.putExtra(Intent.EXTRA_TEXT, shareText);
startActivity(Intent.createChooser(shareIntent,"Share Wallpaper using"));
于 2017-05-23T12:52:26.777 回答
3
String text = "Look at my awesome picture";
Uri pictureUri = Uri.parse("file://my_picture");
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, text);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/*");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "Share images..."));
于 2018-05-26T07:55:26.367 回答
2

检查下面对我有用的代码

    Picasso.with(getApplicationContext()).load("image url path").into(new Target() {

           @Override

            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                Intent i = new Intent(Intent.ACTION_SEND);
                i.putExtra(Intent.EXTRA_TEXT, "Let me recommend you this application" +
                        "\n"+ "your share url or text ");
                i.setType("image/*");
                i.putExtra(Intent.EXTRA_STREAM, getLocalBitmapUri(bitmap));
                context.startActivity(Intent.createChooser(i, "Share using"));
            }

            @Override
            public void onBitmapFailed(Drawable errorDrawable) {
            }

            @Override
            public void onPrepareLoad(Drawable placeHolderDrawable) {
            }
        });


     private Uri getLocalBitmapUri(Bitmap bmp) {
      Uri bmpUri = null;
      try {
          File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), "share_image_" + System.currentTimeMillis() + ".png");
        FileOutputStream out = new FileOutputStream(file);
        bmp.compress(Bitmap.CompressFormat.PNG, 50, out);
        out.close();
        bmpUri = Uri.fromFile(file);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bmpUri;
}
于 2019-03-04T10:51:07.090 回答
2
private void shareImage(){

    StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
    StrictMode.setVmPolicy(builder.build());

    Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.drawable.Starlay_straightface_image);


    File f =  new File(getExternalCacheDir()+"/"+getResources().getString(R.string.app_name)+".png");
    Intent shareIntent;


    try {
        FileOutputStream outputStream = new FileOutputStream(f);
        bitmap.compress(Bitmap.CompressFormat.PNG,100,outputStream);

        outputStream.flush();
        outputStream.close();
        shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.setType("image/*");
        shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f));
        shareIntent.putExtra(Intent.EXTRA_TEXT,"Hey please check this application " + "https://play.google.com/store/apps/details?id=" +getPackageName());
        shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);


    }catch (Exception e){
        throw new RuntimeException(e);
    }
    startActivity(Intent.createChooser(shareIntent,"Share Picture"));
}
于 2020-10-07T11:07:03.120 回答
1

要共享可绘制图像,必须首先将图像保存在设备的缓存或外部存储中。

我们检查缓存中是否已经存在“shareable_image.jpg”,如果存在,则从现有文件中检索路径。

否则,可绘制图像将保存在缓存中。

然后使用意图共享保存的图像。

private void share() {
    int sharableImage = R.drawable.person2;
    Bitmap bitmap= BitmapFactory.decodeResource(getResources(), sharableImage);
    String path = getExternalCacheDir()+"/sharable_image.jpg";
    java.io.OutputStream out;
    java.io.File file = new java.io.File(path);

    if(!file.exists()) {
        try {
            out = new java.io.FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
            out.flush();
            out.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    path = file.getPath();

    Uri bmpUri = Uri.parse("file://" + path);

    Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
    shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    shareIntent.putExtra(Intent.EXTRA_TEXT, "This is a sample body with more detailed description");
    shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
    shareIntent.setType("image/*");
    startActivity(Intent.createChooser(shareIntent,"Share with"));
}
于 2019-07-10T09:44:05.407 回答
1

我在 Android X 上尝试了这个解决方案,并且与 whatsapp、telegram 和 gmail 完美配合。这是我的代码:

Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, "<<sharingText>>");
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("<<imageResource>>"));
shareIntent.setType("image/*");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
if (getView() != null && getView().getContext() != null &&       
    shareIntent.resolveActivity(getView().getContext().getPackageManager()) != null) {                 
    getView().getContext().startActivity(Intent.createChooser(shareIntent, null));
}
于 2021-05-24T12:59:14.130 回答
0

您可以使用此代码

Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.refer_image);
    Intent share = new Intent(Intent.ACTION_SEND);
    share.setType("image/*" + "text/*");
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    b.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = MediaStore.Images.Media.insertImage(this.getContentResolver(),
            b, "Title", null);
    Uri imageUri = Uri.parse(path);
    share.putExtra(Intent.EXTRA_STREAM, imageUri);
    share.putExtra(Intent.EXTRA_TEXT, "Hey I am Subhajit");
    startActivity(Intent.createChooser(share, "Share via"));
于 2022-02-01T09:35:24.317 回答
0

偶然(短信部分,我已经放弃了),我注意到当我选择 Messages App 来处理请求时,Message App 会打开 Intent.EXTRA_SUBJECT 中的文本以及准备发送的图像,我希望它有助于。

String[] recipient = {"your_email_here"};
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_EMAIL, recipient);
intent.putExtra(Intent.EXTRA_SUBJECT, "Hello, this is the subject line");
intent.putExtra(Intent.EXTRA_TEXT, messageEditText.getText().toString());
intent.putExtra(Intent.EXTRA_STREAM, imageUri);
intent.setType("image/*");
于 2016-05-28T06:06:30.937 回答