如何在Android中使用ACTION_SEND共享图像+文本?

时间:2022-11-15 12:13:01

I want to share Text + Image together using ACTION_SEND in android, I am using below code, I can share only Image but i can not share Text with it,

我想在android中使用ACTION_SEND共享Text + Image,我使用下面的代码,我只能共享Image但我不能与它共享Text,

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

Any help on this ?

对此有何帮助?

7 个解决方案

#1


37  

you can share plain text by these codes

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

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

so your full code (your image+text) becomes

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

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

I just replaced image/* with */*

我刚用* / *替换了image / *

update:

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

#2


6  

It is possibly because the sharing app (FB, twitter, etc) may not have permissions to read the image.

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

Google's document says:

谷歌的文件说:

The receiving application needs permission to access the data the Uri points to. The recommended ways to do this are:

接收应用程序需要权限才能访问Uri指向的数据。建议的方法是:

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

I am not sure the sharing apps have permissions to read an image in the bundle of our apps. But my files saved in

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

 Activity.getFilesDir()

cannot be read. As suggested in the above link, we may consider to store images in the MediaStore, where the sharing apps have permissions to read.

无法阅读。如上述链接所示,我们可能会考虑将图像存储在MediaStore中,共享应用程序有权阅读。

Update1: The following is working code to share image with text in Android 4.4.

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

The side effect is we add an image in the MediaStore.

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

#3


6  

please have a look on this code worked for me to share an text and image together

请看看这个代码,我可以一起分享文字和图像

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

Don't forget to give WRITE_EXTERNAL_STORAGE Permission

不要忘记给WRITE_EXTERNAL_STORAGE权限

also in facebook it can only share the image because facebook is not allowing to share the text via intent

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

#4


1  

try using this code.I am uploading ic_launcher from drawable.you can change this with your file from gallary or bitmap.

尝试使用此代码。我从drawable上传ic_launcher。您可以使用gallary或bitmap中的文件更改此内容。

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

#5


0  

By accident(the text message part, I had given up on that), I noticed that when I chose Messages App to handle the request, the Message App would open with the text from Intent.EXTRA_SUBJECT plus the image ready to send, I hope it helps.

偶然(文本消息部分,我放弃了),我注意到当我选择消息应用程序来处理请求时,消息应用程序将打开来自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/*");

#6


0  

I have been looking for solution of this question for a while and found this one up and running, hope it helps.

我一直在寻找这个问题的解决方案一段时间,并发现这个问题,并希望它有所帮助。

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

#7


0  

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

#1


37  

you can share plain text by these codes

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

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

so your full code (your image+text) becomes

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

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

I just replaced image/* with */*

我刚用* / *替换了image / *

update:

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

#2


6  

It is possibly because the sharing app (FB, twitter, etc) may not have permissions to read the image.

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

Google's document says:

谷歌的文件说:

The receiving application needs permission to access the data the Uri points to. The recommended ways to do this are:

接收应用程序需要权限才能访问Uri指向的数据。建议的方法是:

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

I am not sure the sharing apps have permissions to read an image in the bundle of our apps. But my files saved in

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

 Activity.getFilesDir()

cannot be read. As suggested in the above link, we may consider to store images in the MediaStore, where the sharing apps have permissions to read.

无法阅读。如上述链接所示,我们可能会考虑将图像存储在MediaStore中,共享应用程序有权阅读。

Update1: The following is working code to share image with text in Android 4.4.

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

The side effect is we add an image in the MediaStore.

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

#3


6  

please have a look on this code worked for me to share an text and image together

请看看这个代码,我可以一起分享文字和图像

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

Don't forget to give WRITE_EXTERNAL_STORAGE Permission

不要忘记给WRITE_EXTERNAL_STORAGE权限

also in facebook it can only share the image because facebook is not allowing to share the text via intent

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

#4


1  

try using this code.I am uploading ic_launcher from drawable.you can change this with your file from gallary or bitmap.

尝试使用此代码。我从drawable上传ic_launcher。您可以使用gallary或bitmap中的文件更改此内容。

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

#5


0  

By accident(the text message part, I had given up on that), I noticed that when I chose Messages App to handle the request, the Message App would open with the text from Intent.EXTRA_SUBJECT plus the image ready to send, I hope it helps.

偶然(文本消息部分,我放弃了),我注意到当我选择消息应用程序来处理请求时,消息应用程序将打开来自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/*");

#6


0  

I have been looking for solution of this question for a while and found this one up and running, hope it helps.

我一直在寻找这个问题的解决方案一段时间,并发现这个问题,并希望它有所帮助。

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

#7


0  

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