Android版本兼容(存储、http,相机、SparseArray、升级)

版本兼容处理(后续我会一直迭代下去的,只要遇到了就更新,希望能帮到大家)

Android 11 存储规范

Android 11 中的存储机制更新

Android 10(API 29),在Android 10设备操作文件无效,临时解决方案为在AndroidManifest.xml中application标签添加以下属性:
注:如果你无法添加requestLegacyExternalStorage="true"属性,请查看你的targetSdkVersion,只有在28以上才有


 http请求支持

Android 9.0(API 27),如进行http请求,需AndroidManifest.xml中application标签添加以下属性:



net_config.xml



    

Android7.0调用系统相机图片路径处理 

1.首先你需要在你的res路径新建一个xml文件夹,并新建一个filepath.xml文件(命名可修改)

2.在你的filepath.xml里面添加存储设置






3.然后在manifest文件里面添加provide配置 


    

 Android10关于SparseArray转json不支持的问题

建议使用map,不使用SparseArray

SparseArray> sparseArray = new SparseArray<>();
Map map = new HashMap<>();
Map> map1 = new HashMap<>();
map.put(100,"大爷");
sparseArray.put(1,map);
map1.put(2,map);
String mapJson = GsonUtil.beanToJSONString(map);
String map1Json = GsonUtil.beanToJSONString(map1);
String sparseJson = GsonUtil.beanToJSONString(sparseArray);
Log.i("sss",mapJson);
Log.i("sss",map1Json);
Log.i("sss",sparseJson);

以上三个值分别为:

mapJson:{"100":"大爷"}
map1Json:{"2":{"100":"大爷"}}
sparseJson:{}

Android9的升级适配

此处9之前版本intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);可生效,9含之后就只能addflag了,否则提示解析包异常

public static boolean install(Context context, File file) {
        Log.i("install", "安装包路径为:" + file.getPath());
        try {
            if (context != null && file != null) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                if (Build.VERSION.SDK_INT >= 24) {
                    Uri apkUri = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".fileProvider", file);
                    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
                } else {
                    intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
                }
                context.startActivity(intent);
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

 

你可能感兴趣的:(分享,技术,Android10,兼容,高版本,网络https,相机,集合)