Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于升级Android11的一些修改 #316

Open
Yx-s opened this issue Oct 25, 2023 · 2 comments
Open

关于升级Android11的一些修改 #316

Yx-s opened this issue Oct 25, 2023 · 2 comments

Comments

@Yx-s
Copy link

Yx-s commented Oct 25, 2023

看完都给我点赞加关注
不懂Q喔957683077

@Yx-s
Copy link
Author

Yx-s commented Oct 25, 2023

1.新增权限
——安卓11及以后外部存储权限申请 - Google Play商店有一项限制MANAGE_EXTERNAL_STORAGE使用的策略

<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
 tools:ignore="ScopedStorage" />

2.打开相册前:先授权管理所有文件权限

   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            // 管理所有文件权限
            if (!Environment.isExternalStorageManager()) {
                // 没权限提示授权
                DialogConfirmUtil.showConfirmDefault(this, "温馨提示",
                        "需手动授予文件管理权限,点击‘确定’跳转到设置页~",
                        new DialogConfirmUtil.OnConfirmCallback() {
                            @Override
                            public void onSuccess(String str) {
                                   try {
                                           // 跳转到系统设置授权页,授权完毕用户返回重新点击打开相册
                                            Intent appIntent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
                                            appIntent.setData(Uri.parse("package:" + getPackageName()));
                                           // appIntent.setData(Uri.fromParts("package", activity.getPackageName(), null));
                                          startActivity(appIntent);
                                    } catch (Exception ex) {
                                          ex.printStackTrace();
                                          try {
                                                 Intent allFileIntent = new Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
                                                 startActivity(allFileIntent);
                                          } catch (Exception e) {
                                                 e.printStackTrace();
                                          }
                                   }
                            }
                            @Override
                            public void onFail(String str) {
                            }
                        });
            }
        }

3.上面给完权限,接着判断一次正常的那些读写相机权限,相机的修改部分暂时没改,估计要升级Camera2。
4.修改源码:MediaUtils.java类中的getMediaWithImageList()方法67行处开始,代码如下

   // Cursor cursor = contentResolver.queryxxxxxxxxx源码这开始;
  // 改为:
        Cursor cursor = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            String[] strings = projection.toArray(new String[projection.size()]);
            Bundle queryArgs = new Bundle();
            // selection
            if (selection != null) {
                queryArgs.putString(ContentResolver.QUERY_ARG_SQL_SELECTION, selection);
            }
            // selectionArgs
            if (selectionArgs != null) {
                queryArgs.putStringArray(ContentResolver.QUERY_ARG_SQL_SELECTION_ARGS, selectionArgs);
            }
            // 解决方法
            queryArgs.putStringArray(ContentResolver.QUERY_ARG_SORT_COLUMNS, new String[]{MediaStore.Images.Media.DATE_ADDED});
            queryArgs.putInt(ContentResolver.QUERY_ARG_SORT_DIRECTION, ContentResolver.QUERY_SORT_DIRECTION_DESCENDING);
            if (limit > 0) {
                queryArgs.putInt(ContentResolver.QUERY_ARG_LIMIT, limit);
            }
            queryArgs.putInt(ContentResolver.QUERY_ARG_OFFSET, offset);
            // 实现查询
            try {
                cursor = contentResolver.query(uri, strings, queryArgs, null); // CancellationSignal提供取消正在进行的操作的功能。
            } catch (Exception e) {
                // 异常:
                Logger.iYx("-------- Android 11 照片读取异常:" + e);
            }
        } else {
            // 源码 - 支持以前的相册
            cursor = contentResolver.query(
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection.toArray(new String[projection.size()]), selection,
                    selectionArgs, MediaStore.Images.Media.DATE_ADDED + " DESC LIMIT " + limit + " OFFSET " + offset);
        }
      if (cursor != null) {......后面代码保持不变


5.有什么不懂加我Q957683077

@JackdawForever
Copy link

JackdawForever commented Dec 6, 2023

在你的基础上我解决了相机问题,本质上是因为android 11 软件包可见性导致resolveActivity==null出现的
所以需要在Manifest中配置queries就可以了

<application
    ...>
    ...
</application>
 
<queries>
    <intent>
        <action android:name="android.media.action.IMAGE_CAPTURE"/>
    </intent>
    <intent>
        <action android:name="android.media.action.VIDEO_CAPTURE"/>
    </intent>
</queries>

关于queries标签需要特定版本
这样修改了library包后导入到自己的工程就行了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants