1

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

Wicked161089
  • 481
  • 4
  • 18

1 Answers1

2

Did you actually send the first image or did you cancel it? I have the same problem and i just realized that the correct image will be send if you continue. I guess the problem is caused by old thumbnail pictures. You could use different filenames.

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

Later you can delete all files starting with "temporary_file"

for (File file : Environment.getExternalStorageDirectory().listFiles()) {
            if (file.isFile() && file.getName().startsWith("temporary_file")) file.delete();
        }
Andree
  • 36
  • 3
  • Oh Thanks! I will try that and give any feedback. But it sounds quite plausible, because I didn't send it, like you expected – Wicked161089 Apr 26 '15 at 20:44
  • I did work! I just create new temporary files and use them instead of overwriting a single one.. in Activitys onDestroy I delete all tmp files. Thanks @Andree – Wicked161089 Apr 28 '15 at 21:02