feat: 添加系统相册入口及重置默认打开方式功能

【功能更新】
1. 系统相册按钮:
   - 工具栏新增“系统相册”菜单项 (ic_cover)。
   - 点击后通过 Intent 调用系统默认相册。

2. 重置按钮:
   - 工具栏新增“重置”菜单项 (ic_cover_reset)。
   - 清除图库 ACTION_VIEW 的默认打开记录,恢复应用选择框。
   - 兼容处理:API <= 30 自动清除,API > 30 提示适配限制说明。

【代码变更】
- MainActivity.java: 新增 action_gallery 与 action_reset_gallery 响应逻辑,包含异常捕获。
- menu_main.xml: 注册两个新菜单项。
- strings.xml: 新增 "system_gallery", "reset_gallery" 字符串资源。

【其他新增】
- 新增 ColorPaletteDialog 类及 dialog_color_palette.xml 布局。
- 新增 styles.xml 样式 ColorPaletteDialog。
- 新增重置图标资源 ic_cover_reset.xml。
- 更新 build.properties 版本配置。
This commit is contained in:
2026-04-30 22:54:44 +08:00
parent ffea383a4e
commit 0788a52652
8 changed files with 128 additions and 3 deletions

View File

@@ -289,7 +289,32 @@ private void loadAlbums() {
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
if (id == R.id.action_gallery) {
Intent intent = new Intent(Intent.ACTION_VIEW, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivity(intent);
return true;
} else if (id == R.id.action_reset_gallery) {
// 清除 图库/ACTION_VIEW 图片 Uri 的默认打开记录
PackageManager pm = getPackageManager();
// 构建和你跳转一模一样的 Intent
Intent clearIntent = new Intent(Intent.ACTION_VIEW, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
try {
// 仅在 API <= 30 时执行重置操作
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.R) {
String mimeType = clearIntent.resolveType(getContentResolver());
pm.clearPackagePreferredActivities(mimeType);
Toast.makeText(this, "已清除默认打开方式", Toast.LENGTH_SHORT).show();
} else {
// API > 30 时,弹出提示说明适配限制
Toast.makeText(this, "应用开发资源有限,本应用适配目标安卓版本为 (" + Build.VERSION_CODES.R + ")。", Toast.LENGTH_LONG).show();
}
} catch (SecurityException e) {
Toast.makeText(this, "需要授予重置默认应用权限,请在系统设置中开启", Toast.LENGTH_LONG).show();
}
return true;
} else if (id == R.id.action_settings) {
startActivity(new Intent(this, SettingsActivity.class));
return true;
} else if (id == R.id.action_about) {

View File

@@ -0,0 +1,44 @@
package cc.winboll.studio.gallery.dialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.TextView;
import androidx.annotation.NonNull;
import cc.winboll.studio.gallery.R;
/**
* 颜色表对话框
* 继承于普通对话框类,使用视图文件
*/
public class ColorPaletteDialog extends Dialog {
public ColorPaletteDialog(@NonNull Context context) {
super(context, R.style.ColorPaletteDialog);
}
public ColorPaletteDialog(@NonNull Context context, int themeResId) {
super(context, themeResId);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_color_palette);
TextView titleText = findViewById(R.id.title_text);
WindowManager.LayoutParams params = getWindow().getAttributes();
params.width = WindowManager.LayoutParams.MATCH_PARENT;
getWindow().setAttributes(params);
}
public void setTitle(String title) {
}
public interface OnColorItemClick {
void onColorClick(int color);
}
}