簡體   English   中英

圖像文件不會覆蓋

[英]Image file does not overwrite

我有這個圖像字節數組,它是從我的相機中獲得的,我將它轉換為一個文件,然后發送回我的其他活動以設置一個圖像視圖。

返回主要活動的相機活動文件

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

我的問題是,如果我第一次正確執行此代碼圖像設置,但如果我嘗試拍攝另一張照片並返回到主要活動圖像仍然相同

這是因為 glide 會在第一次加載時緩存圖像,並且每當您第二次請求時,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); 

所以這確保總是滑行加載最新的圖像。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM