【问题标题】:Sharing image on whatsapp broken after recent update on 22nd may在 5 月 22 日最近更新后,whatsapp 上的共享图像被破坏
【发布时间】:2016-09-27 05:36:41
【问题描述】:

我正在使用通用图像加载器,并且能够使用此代码将图像从我的应用程序转发到 whatsapp

public static void shareImage(Context context, File pictureFile, String text) {
    Uri imageUri = Uri.parse(pictureFile.getAbsolutePath());
    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
    shareIntent.putExtra(Intent.EXTRA_TEXT, text);

    shareIntent.setType("image/*");
    context.startActivity(Intent.createChooser(shareIntent, "Share"));
}

但是随着最近 5 月 22 日的 whatsapp 更新。我收到“不支持文件格式”toast。

【问题讨论】:

  • 那么您是否尝试过更改文件格式?
  • 我的应用程序大多包含 jpg 和 png 。同样的代码以前也可以工作。
  • 是的,现在他们显然改变了支持的格式,所以你也需要改变
  • 是的。图像共享过去可以在不指定扩展名的情况下工作。
  • 那又怎样?这就是第三方集成所发生的情况。您不能指望您的代码无论发生什么变化都能一直工作。

标签: image android-intent sharing whatsapp universal-image-loader


【解决方案1】:

早期的图像共享适用于

shareIntent.setType("image/*");

但在最新更新中,在 whatsapp 上共享图像需要进行以下更改

public class ImageFileNameGenerator implements FileNameGenerator {
    @Override
    public String generate(String imageUri) {
        String extension = imageUri.substring(imageUri.lastIndexOf("."));
        return String.valueOf(imageUri.hashCode() + extension);
    }
}

更新配置

ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
        .defaultDisplayImageOptions(defaultOptions)
        .diskCacheFileNameGenerator(new ImageFileNameGenerator())
        .build();
ImageLoader.getInstance().init(config);

图片分享代码

public static void shareImage(Context context, File pictureFile, String text) {
    String imagePath = pictureFile.getAbsolutePath();

    Uri imageUri = Uri.parse(imagePath);
    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
    shareIntent.putExtra(Intent.EXTRA_TEXT, text);

    String imageFileExtension = imagePath.substring(imagePath.lastIndexOf("."));

    shareIntent.setType("image/" + imageFileExtension);
    shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    context.startActivity(Intent.createChooser(shareIntent, "Share"));
}

【讨论】:

    猜你喜欢
    • 2021-10-02
    • 1970-01-01
    • 2014-09-05
    • 1970-01-01
    • 2011-04-16
    • 2013-12-23
    • 1970-01-01
    • 2016-09-28
    • 1970-01-01
    相关资源
    最近更新 更多