【问题标题】:Send Image via Intent by WhatsApp - Wrong Image通过 WhatsApp 的 Intent 发送图像 - 错误的图像
【发布时间】:2015-04-16 13:17:18
【问题描述】:

我认为这是一个流行的问题,但我找不到与它相关的任何内容。所以这就是我想要的: 我正在尝试使用以下代码通过 WhatsApp 发送图像:

public static void shareImage(Context context,Bitmap bitmap, String text){
    Intent share = new Intent(Intent.ACTION_SEND);
    share.setType("image/*");
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    File f = new File(Environment.getExternalStorageDirectory() + File.separator + "temporary_file.jpg");
    try {
        f.createNewFile();
        FileOutputStream fo = new FileOutputStream(f);
        fo.write(bytes.toByteArray());
        fo.close();
    } catch (IOException e) {                       
            e.printStackTrace();
    }
    share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f));
    share.setPackage("com.whatsapp");
    context.startActivity(Intent.createChooser(share, "Share!"));
}

它工作正常,我第一次使用该应用程序.. 正确的图像出现在 WhatsApp 中。如果我使用我的应用选择另一个位图,Whatsapp 仍会显示第一个图像。

我做了什么以及我认为导致问题的原因:

  • 如果临时文件存在,我删除了f.delete()
  • 然后我用 MediaScanner 更新了 Gallery,因为我认为我放在 Extra 中的 URI 不是最新的......

仅供参考:临时文件包含正确的图像:如果我使用 FileExplorer 选择它,它会显示正确的图像.. 如果我尝试发送图像.. 仍然是旧图像

有人知道问题可能是什么吗?乌里错了吗?如果我打印Uri.fromFile(f)它会说file:///storage/sdcard0/temporary_file.jpg

谢谢!

妮可

【问题讨论】:

    标签: android android-intent bitmap whatsapp


    【解决方案1】:

    您实际上是发送了第一张图片还是取消了它?我有同样的问题,我刚刚意识到如果您继续,将发送正确的图像。我猜这个问题是由旧的缩略图引起的。您可以使用不同的文件名。

    File f = new File(Environment.getExternalStorageDirectory() + File.separator + "temporary_file"+ System.currentTimeMillis() +".jpg");
    

    稍后您可以删除所有以“temporary_file”开头的文件

    for (File file : Environment.getExternalStorageDirectory().listFiles()) {
                if (file.isFile() && file.getName().startsWith("temporary_file")) file.delete();
            }
    

    【讨论】:

    • 哦,谢谢!我会尝试并提供任何反馈。但这听起来很合理,因为我没有像你预期的那样发送它
    • 我确实工作了!我只是创建新的临时文件并使用它们而不是覆盖单个文件。在 Activitys onDestroy 中,我删除了所有 tmp 文件。谢谢@Andree
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-27
    • 1970-01-01
    • 2019-11-19
    • 2014-10-04
    • 2013-06-24
    • 2012-08-27
    • 2014-01-16
    相关资源
    最近更新 更多