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

本文介绍了Android版本兼容性问题的处理,包括Android 11的存储规范,如何在Android 10上处理http请求,Android 7.0调用系统相机获取图片路径的方法,解决Android 10中SparseArray转换JSON的问题,以及Android 9的升级适配策略。提供了解决这些问题的具体步骤和代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

Android 11 存储规范

Android 11 中的存储机制更新

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

<application
    android:allowBackup="true"
    android:icon="@drawable/logo"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:requestLegacyExternalStorage="true">
</application>

 http请求支持

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

<application
    android:allowBackup="true"
    android:icon="@drawable/logo"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:usesCleartextTraffic="true">
</application>

<application
    android:allowBackup="true"
    android:icon="@drawable/logo"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:networkSecurityConfig="@xml/net_config">
</application>

net_config.xml

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true" />
</network-security-config>

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

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

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

<?xml version="1.0" encoding="utf-8"?>
<resources>
<paths>
<external-path name="camera_photos" path="" />
</paths>
</resources>

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

<provider
    android:name="androidx.core.content.FileProvider"
    android:authorities="${applicationId}.provider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
    android:name="android.support.FILE_PROVIDER_PATHS"
    android:resource="@xml/file_paths" />
</provider>

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

建议使用map,不使用SparseArray

SparseArray<Map<Integer,String>> sparseArray = new SparseArray<>();
Map<Integer,String> map = new HashMap<>();
Map<Integer,Map<Integer,String>> 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;
    }

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值