<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (!hasAllPermissionsGranted(grantResults)) {
return;
}
switch (requestCode) {
case 0:
Toast.makeText(getApplicationContext(), "已申请权限", Toast.LENGTH_SHORT).show();
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
解决方式:
1.使用Android10规定的文件夹做操作
2.将gradle中的编译环境,SDK的版本号修改到29以下
3.清单文件加入下面这句停用分区存储即可随意创建文件夹
<application
android:requestLegacyExternalStorage="true">
</application>
测试 (2)和(3)在SDcard根目录成功创建文件
public static String rootSDPath(Context context){
String rootPath = "" ;
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
File rootFile = Environment.getExternalStorageDirectory();
rootPath = rootFile.getPath();
}else {
File cacheDir=context.getCacheDir() ;
rootPath = cacheDir.getPath();
}
try {
PackageManager packageManager = context.getPackageManager() ;
ApplicationInfo info = packageManager.getApplicationInfo(context.getPackageName(), 0);
File file = new File(rootPath + File.separator + info.loadLabel(packageManager));
if (!file.exists()){
file.mkdir();
}
Log.v(TAG,"[rootPath]->file="+info.loadLabel(packageManager));
return file.getPath();
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return "";
}
|