Android 10 适配的常见问题及解决方案
一、Uri适配
在Android 10 中打开本地资源你会发现这种错误, file:// 不被允许作为一个附加的 Uri 的意图,否则会抛出 FileUriExposedException
android.os.FileUriExposedException: file:///storage/emulated/0/
该如何处理呢?
1.在manifest中申明如下:
<provider android:authorities="你的包名.fileprovider" #一定要是包名否则没有授权哦。 android:name="android.support.v4.content.FileProvider" android:grantUriPermissions="true" android:exported="false"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/filepaths"/>provider>
2.在res下新建xml 文件夹,并在其中新建一个filepaths.xml的文件
<?xml version="1.0" encoding="utf-8"?><paths> <root-path path="." name="external_storage_root" />paths>
3.适配打开的Uri
Intent intent = new Intent();Uri uri = null;if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { uri = FileProvider.getUriForFile(context, "你的包名.fileprovider", new File(filesPath)); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);//申请你的权限。用完之后会自动释放。} else { uri = Uri.parse("file://" + filesPath);}intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);intent.setAction(Intent.ACTION_VIEW);
若你想为此建立一个公共的lib或者动态获取这些包名,那如何操作呢?
你需要在第一步中,设置authorities为android:authorities="${applicationId}.fileprovider",并在第三步中设置FileProvider.getUriForFile(context, context.getPackageName()+".fileprovider", new File(filesPath));
二、Notification导致的崩溃问题
java.lang.IllegalArgumentException: Invalid notification (no valid small icon): Notification(pri=0 ……
解决方法: Android 10中icon不能为空,若是你不想设置,可以设置为-1即可,否则崩溃。
三、.悬浮框权限
报错信息
android.view.WindowManager $ BadTokenException: Unable to add window android.view.ViewRootImpl$W@77869a9 – permission denied for window type 2002
解决方案:
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){ type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY }else{ type = WindowManager.LayoutParams.TYPE_PHONE}
更多相关文章
- Android 网络编程 API笔记 - java.net 包 权限 地址 套接字 相关
- 手把手教你:android调用系统相机、相册功能,适配6.0权限获取以及7.
- Android 使用地图时的权限请求
- android检查权限是否获取