FinalTeam / RxGalleryFinal

图片选择库,单选/多选、拍照、裁剪、压缩,自定义。包括视频选择和录制。
https://github.com/FinalTeam/RxGalleryFinal
2.83k stars 513 forks source link

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

Open Yx-s opened 9 months ago

Yx-s commented 9 months ago

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

Yx-s commented 9 months ago

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 commented 7 months ago

在你的基础上我解决了相机问题,本质上是因为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包后导入到自己的工程就行了