Browse Source

调整文件名称保存

zk 1 year ago
parent
commit
a19b6c49bb

+ 3 - 1
app/src/main/java/com/datarecovery/master/module/imgrecover/ImageRecoverViewModel.java

@@ -10,6 +10,7 @@ import com.atmob.app.lib.base.BaseViewModel;
 import com.atmob.app.lib.livedata.SingleLiveEvent;
 import com.atmob.common.runtime.ContextUtil;
 import com.datarecovery.master.R;
+import com.datarecovery.master.utils.FileUtil;
 import com.datarecovery.master.utils.ImageDeepDetector;
 import com.datarecovery.master.utils.MediaStoreHelper;
 import com.datarecovery.master.utils.ToastUtil;
@@ -18,6 +19,7 @@ import org.reactivestreams.Subscription;
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.UUID;
 import java.util.concurrent.Callable;
 import java.util.concurrent.TimeUnit;
 
@@ -290,7 +292,7 @@ public class ImageRecoverViewModel extends BaseViewModel {
         showLoadingEvent.setValue(true);
         RxJavaUtil.doInBackground(() -> {
             for (ImageDeepDetector.ImageFile item : list) {
-                MediaStoreHelper.saveToSharedStorage(MediaStoreHelper.TYPE_IMAGE, item.newInputStream(), item.getName());
+                MediaStoreHelper.saveToSharedStorage(MediaStoreHelper.TYPE_IMAGE, item.newInputStream(), FileUtil.getCreateFileName(item.getName()));
                 item.setCheck(false);
             }
             return true;

+ 3 - 1
app/src/main/java/com/datarecovery/master/module/preview/PreviewViewModel.java

@@ -9,12 +9,14 @@ import com.atmob.app.lib.base.BaseViewModel;
 import com.atmob.common.runtime.ContextUtil;
 import com.datarecovery.master.R;
 import com.datarecovery.master.utils.BoxingUtil;
+import com.datarecovery.master.utils.FileUtil;
 import com.datarecovery.master.utils.ImageDeepDetector;
 import com.datarecovery.master.utils.MediaStoreHelper;
 import com.datarecovery.master.utils.ToastUtil;
 
 import java.lang.ref.WeakReference;
 import java.util.List;
+import java.util.UUID;
 
 import javax.inject.Inject;
 
@@ -114,7 +116,7 @@ public class PreviewViewModel extends BaseViewModel {
                 return;
             }
             try {
-                MediaStoreHelper.saveToSharedStorage(MediaStoreHelper.TYPE_IMAGE, value.newInputStream(), value.getName());
+                MediaStoreHelper.saveToSharedStorage(MediaStoreHelper.TYPE_IMAGE, value.newInputStream(), FileUtil.getCreateFileName(value.getName()));
                 ToastUtil.show(R.string.export_success, ToastUtil.LENGTH_SHORT);
             } catch (Exception e) {
                 ToastUtil.show(R.string.export_fail, ToastUtil.LENGTH_SHORT);

+ 11 - 14
app/src/main/java/com/datarecovery/master/utils/FileUtil.java

@@ -4,12 +4,11 @@ import android.content.Context;
 import android.net.Uri;
 import android.text.format.Formatter;
 
-import androidx.documentfile.provider.DocumentFile;
 
 import com.atmob.common.runtime.ContextUtil;
 
-import java.io.File;
 import java.util.Locale;
+import java.util.UUID;
 
 public class FileUtil {
 
@@ -33,19 +32,17 @@ public class FileUtil {
         return split[split.length - 1].toUpperCase(Locale.getDefault());
     }
 
-    public static long getFileCreationDateFromUri(Context context, Uri uri, String path) {
-        DocumentFile documentFile = DocumentFile.fromSingleUri(context, uri);
-        if (documentFile != null && documentFile.exists()) {
-            return documentFile.lastModified();
-        } else {
-            if (path != null) {
-                File file = new File(path);
-                return file.lastModified();
-            } else {
-                return 0;
-            }
+
+    public static String getCreateFileName(String fileName) {
+        String uuid = UUID.randomUUID().toString();
+        if (fileName == null) {
+            return uuid + ".jpg";
+        }
+        String[] split = fileName.split("\\.");
+        if (split.length <= 1) {
+            return uuid + ".jpg";
         }
+        return uuid + "." + split[split.length - 1];
     }
 
-
 }

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

@@ -536,10 +536,10 @@ public class ImageDeepDetector {
 
         private String fileType;
 
-        private long createTime;
-
         private boolean isCheck;
 
+        private long lastModified;
+
 
         public ImageFile(XFile xFile) {
             this(xFile, CATEGORY_UNKNOWN);
@@ -564,15 +564,16 @@ public class ImageDeepDetector {
                 this.path = xFile.getPath();
             } catch (Exception ignore) {
             }
+            try {
+                this.lastModified = xFile.lastModified();
+            } catch (Exception ignore) {
+            }
             this.fileType = FileUtil.getImageFileType(name);
             this.sizeDescribe = FileUtil.formatShortBytes(this.size);
         }
 
-        public long getCreateTime() {
-            if (createTime == 0 && uri != null) {
-                this.createTime = FileUtil.getFileCreationDateFromUri(ContextUtil.getContext(), uri, path);
-            }
-            return createTime;
+        public long getLastModified() {
+            return lastModified;
         }
 
         public String getSizeDescribe() {

+ 1 - 1
app/src/main/res/layout/activity_preview.xml

@@ -202,7 +202,7 @@
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:layout_marginStart="12dp"
-                    android:text="@{DateUtil.formatNormalDate(DateUtil.YYYY_MM_DD_HH_MM,previewViewModel.currentImageFile.createTime)}"
+                    android:text="@{DateUtil.formatNormalDate(DateUtil.YYYY_MM_DD_HH_MM,previewViewModel.currentImageFile.lastModified)}"
                     android:textColor="#404040"
                     android:textSize="12sp"
                     app:layout_constraintBaseline_toBaselineOf="@+id/tv_preview_img_name"