I thought that this is a popular problem, but I can't find anything related to it. So here is what I want: I'm trying to send an Image by WhatsApp with the following code:
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!"));
}
It works fine, for the first time I use the app.. the right image appear in WhatsApp. If I choose another Bitmap with my App, Whatsapp still shows the first Image.
What I did and what I thought caused the problem:
- If the temporary file existed, I deleted it by
f.delete()
- Then I updated the Gallery with MediaScanner, because I thought, that the URI I put in Extra is not up to date...
Fyi: The temporary file contains the right Image: If I choose it with an FileExplorer it shows the right image.. if I try to send the Image.. still the old Image
Does anyone has an idea what the problem might be? Is the Uri wrong? If I print Uri.fromFile(f)
it says file:///storage/sdcard0/temporary_file.jpg
Thank you!
Nico