在同一条消息中共享带有一些文本的图像

Sharing an image with some text under it, both in the same message

我想在图片下添加一条文本消息(未打印在图片上),因此当我单击共享按钮时,图像和文本消息将作为一条消息共享,如下所示:

我分享图片的代码如下所示:

public void shareImage(View view) {
    Intent shareIntent = new Intent();
    Uri photoURI = FileProvider.getUriForFile(this, "com.abcd.myapp", theImage);
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.putExtra(Intent.EXTRA_STREAM, photoURI);
    shareIntent.setType("image/*");
    shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    startActivity(Intent.createChooser(shareIntent, "Share Image with ..."));
}

如何在同一短信或电子邮件等中添加图片下方的文本 Image created by: abcd.com(作为未打印在图片上的文本)

下面的代码片段应该可以工作。我们将图像添加到 MediaStore。

public void shareImage(View view) {
    Intent shareIntent = new Intent();
    shareIntent.putExtra(Intent.EXTRA_TEXT, Constant.SHARE_MESSAGE
        + Constant.SHARE_URL);
    Uri photoURI = FileProvider.getUriForFile(this, "com.abcd.myapp", theImage);
    Bitmap bm = BitmapFactory.decodeFile(photoURI);
    String url= MediaStore.Images.Media.insertImage(this.getContentResolver(), bm, "title", "description");
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(url));
    shareIntent.setType("image/*");
    shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    startActivity(Intent.createChooser(shareIntent, "Share Image with ..."));
}

试试这个

    Uri imgUri = Uri.fromFile(new File(DIRECTORY + "/" + fileName));

    //Add this code if you get SecurityException error
    StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
    StrictMode.setVmPolicy(builder.build());

    Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
    shareIntent.setType("image/*");
    shareIntent.putExtra(Intent.EXTRA_STREAM, imgUri);
    shareIntent.putExtra(Intent.EXTRA_TEXT, "Text you want to attach with image.");
    startActivity(Intent.createChooser(shareIntent, "Share Image"));

将位图共享到 Twitter/Whatsapp/Facebook Messenger 的简单案例已解决,感谢@Aniruddh Chandratre 解决方案。 MediaStore.Images.Media.insertImage returns content://media 格式的字符串,即 content://media/external/images/media/610

val savedImageURI = MediaStore.Images.Media.insertImage(
         activity.contentResolver, bitmap, "title", "decription")
val shareIntent = Intent(Intent.ACTION_SEND)
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(savedImageURI))
shareIntent.type = "image/*"
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
activity.startActivity(Intent.createChooser(shareIntent, "Share Image"))