feat: 添加 JsonHelper JSON 索引存储
实现了图片索引的 JSON 文件存储功能 - 保存文件夹扫描结果到 JSON 文件 - 从 JSON 文件加载图片索引 - 智能检测是否需要重新扫描 - 支持文件夹路径哈希命名 - 使用 Gson 进行 JSON 序列化 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
10f96fa99d
commit
b63156cda9
1 file changed
+107
@@ -0,0 +1,107 @@
|
||||
package cn.hugh.bz.gk2.utils;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.Log;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import cn.hugh.bz.gk2.model.WallpaperImage;
|
||||
|
||||
public class JsonHelper {
|
||||
private static final String TAG = "JsonHelper";
|
||||
private static final String INDEX_DIR = "image_indices";
|
||||
|
||||
private final Context context;
|
||||
private final Gson gson;
|
||||
private final File indicesDir;
|
||||
|
||||
public JsonHelper(Context context) {
|
||||
this.context = context;
|
||||
this.gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
this.indicesDir = new File(context.getFilesDir(), INDEX_DIR);
|
||||
if (!indicesDir.exists()) {
|
||||
indicesDir.mkdirs();
|
||||
}
|
||||
}
|
||||
|
||||
public void saveIndex(String folderPath, List<WallpaperImage> images) {
|
||||
String fileName = getIndexFileName(folderPath);
|
||||
File indexFile = new File(indicesDir, fileName);
|
||||
|
||||
IndexData data = new IndexData(folderPath, new File(folderPath).getName(),
|
||||
System.currentTimeMillis(), images.size(), images);
|
||||
|
||||
try (FileWriter writer = new FileWriter(indexFile)) {
|
||||
gson.toJson(data, writer);
|
||||
Log.i(TAG, "Saved index: " + fileName + " with " + images.size() + " images");
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "Failed to save index", e);
|
||||
}
|
||||
}
|
||||
|
||||
public List<WallpaperImage> loadImageIndex(String folderPath) {
|
||||
String fileName = getIndexFileName(folderPath);
|
||||
File indexFile = new File(indicesDir, fileName);
|
||||
|
||||
if (!indexFile.exists()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try (FileReader reader = new FileReader(indexFile)) {
|
||||
IndexData data = gson.fromJson(reader, IndexData.class);
|
||||
return data.images;
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "Failed to load index", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private String getIndexFileName(String folderPath) {
|
||||
int hash = folderPath.hashCode();
|
||||
return "folder_" + Math.abs(hash) + ".json";
|
||||
}
|
||||
|
||||
public boolean needsRescan(String folderPath) {
|
||||
List<WallpaperImage> cached = loadImageIndex(folderPath);
|
||||
if (cached == null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
File folder = new File(folderPath);
|
||||
if (!folder.exists()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
String fileName = getIndexFileName(folderPath);
|
||||
File indexFile = new File(indicesDir, fileName);
|
||||
|
||||
return folder.lastModified() > indexFile.lastModified();
|
||||
}
|
||||
|
||||
private static class IndexData {
|
||||
String folderPath;
|
||||
String folderName;
|
||||
long lastScanned;
|
||||
int imageCount;
|
||||
List<WallpaperImage> images;
|
||||
|
||||
IndexData() {}
|
||||
|
||||
IndexData(String folderPath, String folderName, long lastScanned,
|
||||
int imageCount, List<WallpaperImage> images) {
|
||||
this.folderPath = folderPath;
|
||||
this.folderName = folderName;
|
||||
this.lastScanned = lastScanned;
|
||||
this.imageCount = imageCount;
|
||||
this.images = images;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user