Commit e2fc6b18 authored by fallenstardust's avatar fallenstardust

找不到保存路径时创建

parent a2045025
package cn.garymb.ygomobile.ui.cards.deck_square; package cn.garymb.ygomobile.ui.cards.deck_square;
import static cn.garymb.ygomobile.Constants.CORE_DECK_PATH;
import android.widget.Toast; import android.widget.Toast;
import com.google.gson.Gson; import com.google.gson.Gson;
...@@ -757,12 +759,16 @@ public class DeckSquareApiUtil { ...@@ -757,12 +759,16 @@ public class DeckSquareApiUtil {
} }
String fileFullPath = AppsSettings.get().getDeckDir() + "/" + fileName; String fileFullPath = AppsSettings.get().getDeckDir() + "/" + fileName;
if (!onlineDeck.getDeckType().equals(CORE_DECK_PATH))
fileFullPath = AppsSettings.get().getDeckDir() + "/" + onlineDeck.getDeckType()+ "/" + fileName;
// 保存在线卡组到本地 // 保存在线卡组到本地
boolean saved = DeckSquareFileUtil.saveFileToPath(fileFullPath, onlineDeck.getDeckYdk(), onlineDeck.getDeckUpdateDate()); boolean saved = DeckSquareFileUtil.saveFileToPath(fileFullPath, onlineDeck.getDeckYdk(), onlineDeck.getDeckUpdateDate());
if (!saved) LogUtil.e(TAG, "synchronizeDecks(761) 保存失败!的 云备份卡组: " + fileFullPath); if (!saved) {
LogUtil.e(TAG, "synchronizeDecks(761) 保存失败!的 云备份卡组: " + fileFullPath);
LogUtil.i(TAG, "synchronizeDecks(763) 保存成功√的 云备份卡组: " + fileFullPath); } else {
LogUtil.i(TAG, "synchronizeDecks(763) 保存成功√的 云备份卡组: " + fileFullPath);
}
} }
// 上传本地卡组覆盖在线卡组 // 上传本地卡组覆盖在线卡组
......
...@@ -217,25 +217,40 @@ public class DeckSquareFileUtil { ...@@ -217,25 +217,40 @@ public class DeckSquareFileUtil {
public static boolean saveFile(File file, String content, long modificationTime) { public static boolean saveFile(File file, String content, long modificationTime) {
FileOutputStream fos = null; FileOutputStream fos = null;
try { try {
// 创建文件对象 // 确保父目录存在
fos = new FileOutputStream(file); File parentDir = file.getParentFile();
if (parentDir != null && !parentDir.exists()) {
boolean dirsCreated = parentDir.mkdirs(); // 创建所有缺失的父目录
if (!dirsCreated) {
LogUtil.e(TAG, "无法创建文件目录: " + parentDir.getAbsolutePath());
return false;
}
}
// 创建文件对象(如果文件不存在,会自动创建)
if (!file.exists()) {
boolean fileCreated = file.createNewFile();
if (!fileCreated) {
LogUtil.e(TAG, "无法创建文件: " + file.getAbsolutePath());
return false;
}
}
// 创建文件输出流 // 创建文件输出流
fos = new FileOutputStream(file);
// 写入内容 // 写入内容
fos.write(content.getBytes()); fos.write(content.getBytes(StandardCharsets.UTF_8)); // 使用 UTF-8 编码
fos.flush(); fos.flush();
// 设置指定的最后修改时间 // 设置指定的最后修改时间
boolean timeSet = file.setLastModified(modificationTime); boolean timeSet = file.setLastModified(modificationTime);
if (!timeSet) { if (!timeSet) {
LogUtil.w(TAG, "设置文件修改时间失败: " + file.getPath()); LogUtil.w(TAG, "设置文件修改时间失败: " + file.getPath());
} else { } else {
LogUtil.d(TAG, "设置文件修改时间成功: " + file.getPath());
LogUtil.w(TAG, "设置文件修改时间成功: " + file.getPath());
} }
} catch (Exception e) { } catch (Exception e) {
LogUtil.e(TAG, "保存文件失败", e); LogUtil.e(TAG, "保存文件失败", e);
e.printStackTrace();
return false; return false;
} finally { } finally {
if (fos != null) { if (fos != null) {
......
...@@ -379,7 +379,7 @@ ...@@ -379,7 +379,7 @@
<string name="register">注册萌卡</string> <string name="register">注册萌卡</string>
<string name="sign_out">切换账号</string> <string name="sign_out">切换账号</string>
<string name="deck_square">卡组广场</string> <string name="deck_square">卡组广场</string>
<string name="my_deck_online">备份</string> <string name="my_deck_online">在线备份</string>
<string name="input_contributor_name">输入共享者名称</string> <string name="input_contributor_name">输入共享者名称</string>
<string name="sort_by_time">按时间顺序</string> <string name="sort_by_time">按时间顺序</string>
<string name="sort_by_thumb">按点赞顺序</string> <string name="sort_by_thumb">按点赞顺序</string>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment