1、首先要动态申请文件读写权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
//申请权限
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中的
compileSdkVersion和targetSdkVersion改为28或29(30会有问题)
|