简体   繁体   中英

Image file does not overwrite

I have this image byte array which i got from my camera and i convert it to a file and then send back to my other activity to set an imageview.

Camera activity file giving back to main activity

File myfile = new File(new File(getFilesDir(), PHOTOS), "img1.jpg");

                        deleteFile(myfile.getName());
                        myfile.delete();
                        myfile.createNewFile();
                        myfile.getParentFile().mkdirs();
                        Log.e("convertTofile: ", Thread.currentThread() + "");
                        FileOutputStream fos = null;
                        fos = new FileOutputStream(myfile.getPath());

                        Log.e("convertTofile: ", "writing file");
                        fos.write(myImageArray);
                        fos.flush();

                        fos.getFD().sync();
                        fos.close();

                        Intent intent = new Intent().putExtra("fotouri", Uri.fromFile(myfile));
                        setResult(RESULT_OK, intent);
                        finish();

main activity setting image

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {  Uri myuri = (Uri) data.getExtras().get("fotouri");

   Glide.with(this).load(myuri)
            .asBitmap()
            .transform(new RotateTransformation(this,90))
            .placeholder(R.mipmap.ic_launcher)
            .into(img1); 
 }

My problem is if i execute this code image set correctly for the first time but if i try to take another foto and go back to the Main activity agin image is still the same

This is because glide is caching the image for the first time load and whenever you request for second time glide will load the image from cache.

One of the solution can be to tell the glide not to cache any images , like this below:-

 Glide.with(this).load(myuri)
            .asBitmap()
            .transform(new RotateTransformation(this,90))
            .placeholder(R.mipmap.ic_launcher)
            .diskCacheStrategy(DiskCacheStrategy.NONE)
            .skipMemoryCache(true)
            .into(img1); 

So this makes sure that always glide loads the latest image.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM