适配Android 6、7、8、9、10期间遇到的坑

1、h5 https请求的图片加载不出来;https请求无法访问;

解决办法:

  1. Webview里添加:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

mWebView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);

}
  1. Application里添加:
android:usesCleartextTraffic="true"

2、https无法抓包

参考:

https://blog.csdn.net/woyaochenggong774/article/details/80448698

原因:android权限的问题

修改方法:

  1. 在你主工程的res文件下新建xml(如果有直接可以用),在xml中创建net_security_config.xml
<?xml version="1.0" encoding="utf-8"?>

<network-security-config xmlns:android="http://schemas.android.com/apk/res/android">

    <base-config cleartextTrafficPermitted="true">

        <trust-anchors>

            <certificates src="system" overridePins="true" />

            <certificates src="user" overridePins="true" />

        </trust-anchors>

    </base-config>

</network-security-config>
  1. 在主工程AndroidManifest.xml中的application标签下添加
   android:networkSecurityConfig="@xml/net_security_config"

3、deviceId获取

https://developer.android.google.cn/training/articles/user-data-ids

采取谷歌推荐做法,自定义生成UUID:

@SuppressLint("ApplySharedPref")

    public static String getDeviceId(Context context) {

        SharedPreferences deviceIdSp = context.getApplicationContext()

                .getSharedPreferences(DEVICE_ID_SP_NAME, Context.MODE_PRIVATE);

        String deviceId = deviceIdSp.getString(DEVICE_ID_KEY, "");

        if (TextUtils.isEmpty(deviceId)) {

            deviceId = UUID.randomUUID().toString();

            deviceIdSp.edit().putString(DEVICE_ID_KEY, deviceId).commit();

        }
        return deviceId;
}

4、小米 mi8 Android 9.0上—onTouchEvent的action_up方法不走

解决办法:将手机系统升级到MIUI 11.0.4后问题解决。

5、Android 10上无法在sd上创建文件夹,动态申请过权限,sd卡状态为mounted,Android 9上一切正常

参考资料:
https://blog.csdn.net/yehui928186846/article/details/101706238

https://developer.android.google.cn/reference/android/os/Environment.html#getExternalStorageDirectory()

This method was deprecated in API level 29.
To improve user privacy, direct access to shared/external storage devices is deprecated. When an app targets Build.VERSION_CODES.Q, the path returned from this method is no longer directly accessible to apps. Apps can continue to access content stored on shared/external storage by migrating to alternatives such as Context#getExternalFilesDir(String), MediaStore, or Intent#ACTION_OPEN_DOCUMENT.

6、拍照及从相册选择图片的问题

参考资料:https://juejin.im/post/5d80ef726fb9a06aeb10f223

拍照必须使用file provider;

相册选取的图片不展示的问题:

解决方案:读取bitmap,将其压缩并存储在data/package_name/files 文件夹下,使用bitmapFactory.decodeFile加载bitmap展示在imageview上。

//获取图片Uri

private static Uri getImageContentUri(Context context, String path) {

        Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,

                new String[]{MediaStore.Images.Media._ID}, MediaStore.Images.Media.DATA + "=? ",

                new String[]{path}, null);

        if (cursor != null && cursor.moveToFirst()) {

            int id = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID));

            Uri baseUri = Uri.parse("content://media/external/images/media");

            return Uri.withAppendedPath(baseUri, "" + id);

        } else {

            // 如果图片不在手机的共享图片数据库,就先把它插入。

            if (new File(path).exists()) {

                ContentValues values = new ContentValues();

                values.put(MediaStore.Images.Media.DATA, path);

                return context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

            } else {

                return null;

            }

        }

}

 

				//读取bitmap,并压缩

				BitmapFactory.Options options = new BitmapFactory.Options();

                options.inJustDecodeBounds = true;

 

                Uri imageUri = getImageContentUri(JDSendApp.getInstance(), srcFilePath);

                ParcelFileDescriptor parcelFileDescriptor = context

                        .getApplicationContext()

                        .getContentResolver()

                        .openFileDescriptor(imageUri, "r");

 

                BitmapFactory.decodeFileDescriptor(parcelFileDescriptor.getFileDescriptor(), null, options);

                // 计算采样率

                if (reqHeight == 0) {

                    options.inSampleSize = calculateInSampleSize(options, reqWidth);

                } else {
                    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
                }

                // Decode bitmap with inSampleSize set

                options.inJustDecodeBounds = false;
                return BitmapFactory.decodeFileDescriptor(parcelFileDescriptor.getFileDescriptor(), null, options);

你可能感兴趣的:(Android)