【发布时间】: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