WhatsApp分享的图片只是黑色

WhatsApp shared image just black

if (Utils.isPackageInstalled(getContext(), "com.whatsapp")) {
    Intent sendIntent = new Intent();
    sendIntent.setAction(Intent.ACTION_SEND);

    Bitmap bitmap = Utils.screenShotBitmap(getActivity());
    Utils.saveImage(getActivity(), bitmap, bet.getID() + ".jpeg");
    File file = new File(getActivity().getFilesDir(), bet.getID() + ".jpeg");

    if (file.exists()) {
        Log.i("share", "file exists");
        Log.i("share", Uri.fromFile(file).toString());
    }
    sendIntent.putExtra(Intent.EXTRA_TEXT, "Share text");
    sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
    sendIntent.setType("image/*");

    sendIntent.setPackage("com.whatsapp");
    startActivity(sendIntent);
}

// ...
public static Bitmap screenShotBitmap(Activity activity) {
    return Falcon.takeScreenshotBitmap(activity);
}

public static void saveImage(Context context, Bitmap b, String name){
    FileOutputStream out;

    try {
        out = context.openFileOutput(name, Context.MODE_PRIVATE);
        b.compress(Bitmap.CompressFormat.JPEG, 90, out);
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

日志输出:

03-25 17:13:24.328 26417-26417/x.x.x I/share: file exists
03-25 17:13:24.328 26417-26417/x.x.x I/share: file:///data/data/x.x.x/files/4b00abc2-7aae-4234-945b-59905306ad4a.jpeg

结果:

Bitmap bitmap = Utils.screenShotBitmap(getActivity()); 似乎工作正常并且 return 一个正确的位图,因为我可以毫无问题地将它分享到 facebook。

because I can share it without problems to facebook

不是那个代码。

您正在将文件写入内部存储。 Facebook 和 WhatsApp 都无法访问您的 internal storage. Plus, using file: Uri values is being phased out.

部分

正确的长期答案是use a ContentProvider to publish the bitmap, such as using FileProvider. In the short term, you can probably get away with writing to external storage,例如getExternalFilesDir()

另请注意,ACTION_SEND 收件人不需要同时兑现 EXTRA_TEXTEXTRA_STREAM,而只需兑现其中之一。