安卓在共享存储创建文件报Operation not permitted
本帖最后由 放假几天 于 2022-1-12 21:09 编辑代码
/**
* 创建文件
*
* @Param name
* @return
*/
public String fileCj(String name) {
// 获取外部存储路径
// String path = Environment.getDownloadCacheDirectory().getAbsolutePath()+"/"+name;
// Log.e("路径",Environment.getExternalStorageDirectory()+"");
// Log.e("",Environment.getExternalStorageState().equals("mounted")+"");//查看外部存储是否存在
// String path = context.getExternalFilesDir(null)+ "//" + name;
File createFiles = new File(name);
try {
if (!createFiles.exists()) {
createFiles.createNewFile();
}
} catch (IOException e) {
Log.d("文件创建失败", "files err:" + e.getMessage());
}
return name;
}
报错
W/System.err: java.io.FileNotFoundException: /storage/emulated/0/Documents/gnss/2022-01-12-03:45:55/GNSS/location.txt: open failed: EPERM (Operation not permitted)
没权限 申请存储权限 jxn|android 发表于 2022-1-12 16:52
没权限 申请存储权限
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED
|| ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED
|| ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED
|| ContextCompat.checkSelfPermission(getApplication(), Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
//多个权限一起申请
ActivityCompat.requestPermissions(this, new String[]{
Manifest.permission.CAMERA,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.ACCESS_FINE_LOCATION,
}, 1);
}
给了权限的。文件夹可以创建,但文件不可以 已解决,参考https://stackoverflow.com/questions/68140893/android-studio-throwing-ioexception-operation-not-permitted
原因是Android 11 中的存储机制更新导致 好吧 好久没写安卓了
页:
[1]