Android 共享图像和文本,并与Whats应用程序共享

Android 共享图像和文本,并与Whats应用程序共享,android,share,Android,Share,我正在尝试共享一张来自imageview的图像,其中带有whatsApp的文本标题,但我在网上找到的解决方案似乎对我不起作用 View content = findViewById(R.id.posted_house); content.setDrawingCacheEnabled(true); Bitmap bitmap = content.getDrawingCache(); File root = Environment.getExterna

我正在尝试共享一张来自
imageview
的图像,其中带有whatsApp的文本标题,但我在网上找到的解决方案似乎对我不起作用

View content = findViewById(R.id.posted_house);
        content.setDrawingCacheEnabled(true);
        Bitmap bitmap = content.getDrawingCache();
        File root = Environment.getExternalStorageDirectory();
        File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/image.jpg");
        try
        {
            root.createNewFile();
           FileOutputStream ostream = new FileOutputStream(cachePath);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, ostream);
            ostream.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        Intent txtIntent = new Intent(android.content.Intent.ACTION_SEND);
        txtIntent .setType("image/*");
        txtIntent .putExtra("message");
        txtIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(cachePath));
        startActivity(Intent.createChooser(txtIntent ,"Share"));
    }
  • 下面是在whatsapp中共享图像和文本的代码

    View screenView = rootView.getRootView();
    screenView.setDrawingCacheEnabled(true);
    Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache());
    screenView.setDrawingCacheEnabled(false);
    
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = MediaStore.Images.Media.insertImage(this.getContentResolver(), bitmap, "Title", null);
    
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_SEND);
    intent.setType("image/*");
    
    intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "");
    intent.putExtra(android.content.Intent.EXTRA_TEXT, "Your message");
    intent.putExtra(Intent.EXTRA_STREAM, Uri.parse(path));
    try {
        startActivity(Intent.createChooser(intent, "Share Screenshot"));
    } catch (ActivityNotFoundException e) {
        Toast.makeText(this, "No App Available", Toast.LENGTH_SHORT).show();
    }
    

新建文件输出流(根)
–您正在尝试在
上创建一个
文件输出流
。您的图像文件是
cachePath
,而不是
root
。我编辑了代码,但得到了相同的结果。正在创建的文件在共享预览中显示黑色背景?您是否从
catch
块获取任何日志?您是否具有写入外部存储器的权限?你确定WhatsApp可以处理任意文件吗?