安卓11授权修改如下:
添加新的权限
<!-- 安卓11下载读写内存权限-->
<uses-permission android:name=".MANAGE_EXTERNAL_STORAGE"/>
修改代码
filePath = (Environment.DIRECTORY_DOWNLOADS) + filePath;
import ;
import ;
import ;
import ;
import ;
import ;
@SuppressWarnings("deprecation")
@SuppressLint({ "DefaultLocale", "SimpleDateFormat" })
public class Function_Utility {
private static Context mAppContext;
public static void setAppContext(Context context) {
mAppContext = context;
}
public static Context getAppContext() {
return mAppContext;
}
/**
* 获取下载路径
*/
public static String getUpgradePath() {
String filePath = getAppRootPath() + "";
File file = new File(filePath);
if (!()) {
();
}
file = null;
return filePath;
}
public static String getAppRootPath() {
String filePath = "/downloads";
if (().equals(Environment.MEDIA_MOUNTED)) {
filePath = (Environment.DIRECTORY_DOWNLOADS) + filePath;
} else {
filePath = getAppContext().getCacheDir() + filePath;
}
File file = new File(filePath);
if (!()) {
();
}
file = null;
File nomedia = new File(filePath + "/.nomedia");
if (!())
try {
();
} catch (IOException e) {
();
}
return filePath;
}
}
除了安卓6.0的动态授权外,还需要读写权限做授权
if (.SDK_INT >= Build.VERSION_CODES.R ) {// android 11 且 不是已经被拒绝
// 先判断有没有权限
if (!()) {
Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
(("package:" + getPackageName()));
startActivityForResult(intent, 1024);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
(requestCode, resultCode, data);
if (requestCode == 1024 && .SDK_INT >= Build.VERSION_CODES.R) {
// 检查是否有权限
if (()) {
isRefuse = false;
// 授权成功
(,"权限授权成功");
} else {
isRefuse = true;
// 授权失败
(,"获取权限失败,可能APP更新、下载图片等部分功能无法正常使用,请手动开启");
}
}
}