解决Android10和Android11创建文件失败问题

1、首先要动态申请文件读写权限


//申请权限
int REQUEST_CODE_CONTACT = 101;
String[] permissions = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_PHONE_STATE};
//验证是否申请权限
if (getApplicationContext().checkSelfPermission(permissions[1]) != PackageManager.PERMISSION_GRANTED) {
//申请权限
ActivityCompat.requestPermissions(MainActivity.this, permissions, REQUEST_CODE_CONTACT);
}

2、在AndroidManifest.xml中添加如下配置:

android:requestLegacyExternalStorage="true"

3、将build.gradle中的

compileSdkVersiontargetSdkVersion改为28或29(30会有问题)

你可能感兴趣的:(Android,android,android,studio,java)