Browse Source

[New]图片扫描结束清除缓存文件

zhipeng 1 year ago
parent
commit
c1cbfa093f

+ 28 - 8
app/src/main/java/com/datarecovery/master/utils/ImageDeepDetector.java

@@ -603,7 +603,7 @@ public class ImageDeepDetector {
         }
 
         private boolean isGallery() {
-            if (!TextUtils.isEmpty(path) && (
+            return !TextUtils.isEmpty(path) && (
                     path.contains("com.android.gallery3d") ||
                             path.contains("com.coloros.gallery3d") ||
                             path.contains("com.vivo.gallery") ||
@@ -615,10 +615,7 @@ public class ImageDeepDetector {
                             path.contains("DCIM") ||
                             path.contains("Pictures") ||
                             path.contains(".RecycleBin")
-            )) {
-                return true;
-            }
-            return false;
+            );
         }
     }
 
@@ -762,6 +759,9 @@ public class ImageDeepDetector {
 
         private RandomAccessFile mActiveDataFile;
         private int mActiveHashStart;
+        private File indexTemp;
+        private File data0Temp;
+        private File data1Temp;
 
         public GalleryCacheDetector(Context context, XFile galleryCacheDir) {
             this.context = context;
@@ -859,6 +859,7 @@ public class ImageDeepDetector {
                 subscriber.onError(e);
             } finally {
                 closeAll();
+                deleteTempFiles();
             }
         }
 
@@ -897,13 +898,13 @@ public class ImageDeepDetector {
                  InputStream data0Is = dataFile0.newInputStream();
                  InputStream data1Is = dataFile1.newInputStream()
             ) {
-                File indexTemp = createTempFile("index.temp", idxIs);
+                indexTemp = createTempFile("index.temp", idxIs);
                 mIndexFile = new RandomAccessFile(indexTemp, "rw");
 
-                File data0Temp = createTempFile("data0.temp", data0Is);
+                data0Temp = createTempFile("data0.temp", data0Is);
                 mDataFile0 = new RandomAccessFile(data0Temp, "rw");
 
-                File data1Temp = createTempFile("data1.temp", data1Is);
+                data1Temp = createTempFile("data1.temp", data1Is);
                 mDataFile1 = new RandomAccessFile(data1Temp, "rw");
 
                 // Map index file to memory
@@ -1126,6 +1127,25 @@ public class ImageDeepDetector {
             }
         }
 
+        private void deleteTempFiles() {
+            deleteSilently(indexTemp);
+            deleteSilently(data0Temp);
+            deleteSilently(data1Temp);
+        }
+
+        private void deleteSilently(File tempFile) {
+            if (tempFile == null) {
+                return;
+            }
+            try {
+                if (tempFile.exists()) {
+                    tempFile.delete();
+                }
+            } catch (Throwable t) {
+                // do nothing
+            }
+        }
+
         public static class LookupRequest {
             public long key;        // input: the key to find
             public byte[] buffer;   // input/output: the buffer to store the blob