适配Android 6、7、8、9、10期间遇到的坑
1、h5 https请求的图片加载不出来;https请求无法访问;
解决办法:
- Webview里添加:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {mWebView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);}
- Application里添加:
android:usesCleartextTraffic="true"
2、https无法抓包
参考:
https://blog.csdn.net/woyaochenggong774/article/details/80448698
原因:android权限的问题
修改方法:
- 在你主工程的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>
- 在主工程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上。
//获取图片Uriprivate 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 开发:保存图片到SD卡上
- [Android] 图片JNI(C++\Java)高斯模糊 多线程
- android 调用系统的图片选择
- android中Bitmap图像处理 修改图片大小以及保存时的文件大小
- android 使用反射机制获取工程中的图片
- 设置图片缩放方法失效 解决办法设置android:scaleType属性