简体   繁体   English

图像文件不会覆盖

[英]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.这是因为 glide 会在第一次加载时缓存图像,并且每当您第二次请求时,glide 都会从缓存中加载图像。

One of the solution can be to tell the glide not to cache any images , like this below:-解决方案之一是告诉 glide 不要缓存任何图像,如下所示:-

 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.所以这确保总是滑行加载最新的图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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