首页 > 解决方案 > Android Share Intent Image Sharing 无法正常工作,除了 WhatsApp

问题描述

我检查了uri,保存的文件是位图及其返回文件。但分享时显示的图片无效。Gmail 说它无法附加附件,消息应用程序无法加载图像。

文本共享工作正常。

我是 Android 新手,通过共享意图共享图像时遇到问题。我用谷歌搜索了很多,尝试了各种方法,但仍然找不到解决方案。

public static void shareWallpaper(Context context, int wallpaperResourceId) {

    Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), wallpaperResourceId);
    String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/wallpaper" + String.valueOf(wallpaperResourceId) + ".jpg";
    OutputStream out = null;
    File file = new File(path);
    try {
        out = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
        out.flush();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.setType("image/*");
    shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(file.getPath()));
    shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    shareIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    context.startActivity(Intent.createChooser(shareIntent, "برنامه مورد نظر خود را انتخاب کنید"));


}

标签: javaandroid

解决方案


推荐阅读