我正在使用下面的代码在Android中共享图像和文本。当我选择Whatsapp时,它共享图像和文本,但是当我选择电报时,它仅共享图像而没有任何文本!我的代码有什么问题?特纳克斯

    BitmapDrawable drawable = (BitmapDrawable) imageViewSample .getDrawable();
    Bitmap bitmapImg = drawable.getBitmap();
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    bitmapImg.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = Images.Media.insertImage(getContext() .getContentResolver(), bitmapImg, "Title", null);
    Uri myUri= Uri.parse(path);
        try {
            Intent share = new Intent(Intent.ACTION_SEND);
            share.putExtra(Intent.EXTRA_STREAM , myUri);
            myBodyText="This is a test.";
            share.putExtra(Intent.EXTRA_TEXT , myBodyText);
            share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            share.setType("image/*");
            startActivity(Intent.createChooser(share, "choose app"));
       } catch (Exception e) {
            e.printStackTrace();
 }

最佳答案

使用传递的PATH作为参数创建一个新文件,然后使用类Uri(统一资源标识符)中的“fromFile(文件名)”方法,并照常进行代码处理。

BitmapDrawable drawable = (BitmapDrawable) imageViewSample .getDrawable();
Bitmap bitmapImg = drawable.getBitmap();
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmapImg.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = Images.Media.insertImage(getContext() .getContentResolver(),bitmapImg, "Title", null);
File myImage = new File(path); // introduce the new File
Uri myUri= Uri.fromFile(myImage); //Pass the file as argument
  try {
        Intent share = new Intent(Intent.ACTION_SEND);
        share.putExtra(Intent.EXTRA_STREAM , myUri);
        myBodyText="This is a test.";
        share.putExtra(Intent.EXTRA_TEXT , myBodyText);
        share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        share.setType("image/*");
        startActivity(Intent.createChooser(share, "choose app"));
      } catch (Exception e) {
        e.printStackTrace();
      }

10-08 18:01