fix: 优化历史记录图片加载和错误处理

- 使用 Glide 替代手动加载缩略图,提升加载性能和用户体验
- 增强打开图片的错误处理,添加 file:// URI 保护
- 捕获 ActivityNotFoundException 和 SecurityException 异常
- 提升缩略图尺寸至 500px,提高显示质量

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
hughhuangandClaude Opus 4.5 committed 2026-02-02 16:30:14 +08:00
1 parent f78695077a
commit d5837e76a5
2 files changed
+26 -9

No files matched your search

@@ -133,13 +133,30 @@ public class WallpaperHistoryFragment extends Fragment {
* 点击历史记录,使用系统相册/图片查看器打开 * 点击历史记录,使用系统相册/图片查看器打开
*/ */
private void openAlbumForHistory(WallpaperHistory history) { private void openAlbumForHistory(WallpaperHistory history) {
if (history == null || history.getUri() == null) return;
try { try {
Uri uri = Uri.parse(history.getUri()); Uri uri = Uri.parse(history.getUri());
// 简单检查 scheme,如果是 file 且没有配置 FileProvider,会导致崩溃
// 这里做一个简单的保护,拦截 file:// URI
if ("file".equals(uri.getScheme())) {
Toast.makeText(requireContext(), "不支持直接打开文件路径", Toast.LENGTH_SHORT).show();
return;
}
Intent intent = new Intent(Intent.ACTION_VIEW); Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "image/*"); intent.setDataAndType(uri, "image/*");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // 虽然不是必须,但有些情况下有帮助
startActivity(intent); startActivity(intent);
} catch (android.content.ActivityNotFoundException e) {
Toast.makeText(requireContext(), "未找到可打开图片的应", Toast.LENGTH_SHORT).show();
} catch (SecurityException e) {
Toast.makeText(requireContext(), "没有权限打开此图片", Toast.LENGTH_SHORT).show();
} catch (Exception e) { } catch (Exception e) {
android.util.Log.e("HistoryFragment", "Error opening image", e);
Toast.makeText(requireContext(), "无法打开图片", Toast.LENGTH_SHORT).show(); Toast.makeText(requireContext(), "无法打开图片", Toast.LENGTH_SHORT).show();
} }
} }
@@ -245,14 +262,14 @@ public class WallpaperHistoryFragment extends Fragment {
} }
public void bind(WallpaperHistory history) { public void bind(WallpaperHistory history) {
// 加载缩略图 // 使用 Glide 加载缩略图
Bitmap thumbnail = historyManager.loadThumbnail(history.getThumbnailPath()); com.bumptech.glide.Glide.with(context)
if (thumbnail != null) { .load(history.getThumbnailPath()) // Glide 自动处理文件路径
ivThumbnail.setImageBitmap(thumbnail); .placeholder(R.drawable.ic_image) // 需要一个占位图
} else { .error(R.drawable.ic_image)
// 显示占位符 .centerCrop()
ivThumbnail.setImageDrawable(null); .transition(com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions.withCrossFade())
} .into(ivThumbnail);
// 相册名称 // 相册名称
tvAlbumName.setText(history.getAlbumName()); tvAlbumName.setText(history.getAlbumName());
@@ -30,7 +30,7 @@ public class WallpaperHistoryManager {
private static final String PREFS_NAME = "wallpaper_history_prefs"; private static final String PREFS_NAME = "wallpaper_history_prefs";
private static final String KEY_HISTORY = "history"; private static final String KEY_HISTORY = "history";
private static final int MAX_HISTORY_COUNT = 50; private static final int MAX_HISTORY_COUNT = 50;
private static final int THUMBNAIL_SIZE = 200; private static final int THUMBNAIL_SIZE = 500;
private static final int THUMBNAIL_QUALITY = 85; private static final int THUMBNAIL_QUALITY = 85;
private final SharedPreferences prefs; private final SharedPreferences prefs;