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

Hir*_*tel 37 android intentfilter android-intent android-sharing

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

对此有何帮助?

Ahm*_*kri 49

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

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

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

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

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


小智 11

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

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

不要忘记给WRITE_EXTERNAL_STORAGE权限

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


Ans*_*Yao 6

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

谷歌的文件说:

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

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

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

 Activity.getFilesDir()
Run Code Online (Sandbox Code Playgroud)

无法阅读.如上述链接所示,我们可能会考虑将图像存储在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"));
Run Code Online (Sandbox Code Playgroud)

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

  • 如果共享已完成,您只需从MediaStore中删除图像,即可发生任何事情;-) (3认同)
  • 好点子.如何检测共享已完成? (2认同)