无法共享具有 android 意图的图像

Having trouble sharing an image with an android intent

我正在尝试获取视图(约束布局)的图像并通过 android 发送意图共享它。

我尝试了很多方法,但到现在none都奏效了。 这是我目前所拥有的:

public void shareStatsImage(){
    constraintLayout.setDrawingCacheEnabled(true);
    Bitmap bitmap = constraintLayout.getDrawingCache();
    File path = null;
    try {
        path = saveImageToExternal(generateImageTitle(), bitmap);
    } catch (IOException e) {
        e.printStackTrace();
    }

    final Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.setType("image/png");
    final File photoFile = new File(Objects.requireNonNull(getActivity()).getFilesDir(), Objects.requireNonNull(path).getAbsolutePath());
    shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(photoFile));
    shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    startActivity(Intent.createChooser(shareIntent, "Share image using"));
}
public static String generateImageTitle(){
    SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy-hh-mm-ss");
    return sdf.format(new Date());
}

public File saveImageToExternal(String imgName, Bitmap bm) throws IOException {
    //Create Path to save Image
    String appFolder = "test";
    File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES + File.separator + appFolder); //Creates app specific folder
    path.mkdirs();
    File imageFile = new File(path, imgName+".png"); // Imagename.png
    FileOutputStream out = new FileOutputStream(imageFile);
    try{
        bm.compress(Bitmap.CompressFormat.PNG, 100, out); // Compress Image
        out.flush();
        out.close();

        // Tell the media scanner about the new file so that it is
        // immediately available to the user.
        MediaScannerConnection.scanFile(getContext(),new String[] { imageFile.getAbsolutePath() }, null,new MediaScannerConnection.OnScanCompletedListener() {
            public void onScanCompleted(String path, Uri uri) {
                Log.i("ExternalStorage", "Scanned " + path + ":");
                Log.i("ExternalStorage", "-> uri=" + uri);
            }
        });
    } catch(Exception e) {
        throw new IOException();
    }
    return imageFile;
}

此解决方案存在多个问题,例如,我在将图像共享到 gmail 时收到错误消息 ("Permission denied for the attachment")。将图像上传到 google 驱动器时,我只得到 "upload unsuccessful message".

一件好事是图像似乎出现在 phone 的图库中,只是在通过 intent 分享它们时不会出现 :(

感谢您的帮助!

您可以使用 Uri 发送图片。

Uri imageUri = set your image Uri;

 Intent shareIntent = new Intent();

 shareIntent.setAction(Intent.ACTION_SEND);

 shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);

 shareIntent.setType("image/jpeg");

 shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

 startActivity(Intent.createChooser(shareIntent, "send"));

将位图转换为 Uri

 private Uri getImageUri(Context context, Bitmap inImage) {

    ByteArrayOutputStream bytes = new ByteArrayOutputStream();

    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);

    String path = MediaStore.Images.Media.insertImage(context.getContentResolver(), inImage, "Image Title", null);

    return Uri.parse(path);
}