如何才能第二次正确保存我的位图?

Ers*_*har 6 android android-layout android-imageview android-file android-bitmap

我想将我的位图保存到缓存目录.我用这个代码:

try {
        File file_d = new File(dir+"screenshot.jpg");
        @SuppressWarnings("unused")
        boolean deleted = file_d.delete();
    } catch (Exception e) {
        // TODO: handle exception
    }

    imagePath = new File(dir+"screenshot.jpg");
    FileOutputStream fos;
    try {
        fos = new FileOutputStream(imagePath);
        bitmap.compress(CompressFormat.JPEG, 100, fos);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        Log.e("GREC", e.getMessage(), e);
    } catch (IOException e) {
        Log.e("GREC", e.getMessage(), e);
    }
}
Run Code Online (Sandbox Code Playgroud)

它工作正常.但是,如果我想将不同的img保存到相同的路径,那么就会出现问题.我的意思是它被保存到相同的路径,但我看到它的旧图像,但当我点击图像,我可以看到我第二次保存的正确图像.

也许它来自缓存,但我不想看到旧图像,因为当我想与whatsapp旧图像共享该图像时,如果我发送图像似乎是正确的.

我想在whatsapp上分享已保存的图像,如下代码:

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(imagePath));
shareIntent.setType("image/jpeg");
startActivityForResult(Intent.createChooser(shareIntent, getResources().getText(R.string.title_share)),whtsapp_result);
Run Code Online (Sandbox Code Playgroud)

我该如何解决?

提前致谢.

Ers*_*har 3

最后我这样解决了我的问题:

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, getImageUri(context,bitmap_));
shareIntent.setType("image/jpeg");
startActivityForResult(Intent.createChooser(shareIntent, getResources().getText(R.string.title_share)),whtsapp_result);
Run Code Online (Sandbox Code Playgroud)

v

public Uri getImageUri(Context inContext, Bitmap inImage) {
          ByteArrayOutputStream bytes = new ByteArrayOutputStream();
          inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
          String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "TitleC1", null);
          return Uri.parse(path);
        }
Run Code Online (Sandbox Code Playgroud)