【问题标题】:Image file does not overwrite图像文件不会覆盖
【发布时间】:2018-02-16 02:56:07
【问题描述】:

我有从我的相机获取的图像字节数组,我将其转换为文件,然后发送回我的其他活动以设置图像视图。

相机活动文件回馈给主要活动

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();

主要活动设置图片

@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); 
 }

我的问题是,如果我第一次正确执行此代码图像集,但如果我尝试拍摄另一张照片并返回主活动,则图像仍然相同

【问题讨论】:

    标签: android image file


    【解决方案1】:

    这是因为 glide 正在缓存第一次加载的图像,并且每当您请求第二次时,glide 都会从缓存中加载图像。

    其中一个解决方案是告诉滑翔机不要缓存任何图像,如下所示:-

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

    这样可以确保始终滑动加载最新图像。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-11
      • 1970-01-01
      • 2019-12-27
      • 1970-01-01
      • 2018-08-08
      • 2019-07-14
      • 2020-11-28
      相关资源
      最近更新 更多