![](/img/trans.png)
[英]Failure to read or create a file from external storage even though given read/write permissions
[英]Cannot create a directory inside external storage even though permissions are given
我在 android studio 的外部存储上创建新目录时遇到问题。 它可能必须使用权限做一些事情,但据我所知,一切似乎都很好。
我想做的是:
- 检查目录“/storage/emulated/0/Recordings”是否存在,如果不存在 - 创建目录
- 检查文件“/storage/emulated/0/Recordings/tempFile.raw”是否存在,如果存在 - 删除它
- 使用“new FileOutputStream(filename);”在目录内创建一个文件
我使用的代码(简化):
String filepath = Environment.getExternalStorageDirectory().getPath();
//returns "/storage/emulated/0"
File file = new File(filepath,"Recordings");
if(!file.exists()){
file.mkdirs();
}else{
Log.w(TAG, "Didn't work");
}
fileName= "Recording #" + recordingNumber; //int recordingNumber = 1
recordingNumber++;
absolutePath = file.getAbsolutePath() + "/" + fileName + "tempFile.raw";
try {
os = new FileOutputStream(absolutePath);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
我得到的错误:
java.io.FileNotFoundException: /storage/emulated/0/Recordings/tempFile.raw (No such file or directory)
注意:logcat 中没有“Didn't work”,意思是 file.mkdirs() 被执行了。
清单权限:
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
权限:
虚拟设备上的外部存储:
构建.gradle:
android {
compileSdkVersion 28
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.example.soundrecorder"
minSdkVersion 22
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
首先,您错过了另一个许可:
WRITE_EXTERNAL_STORAGE
此外,您必须明确要求您的活动获得许可。 检查此链接:
https://developer.android.com/training/permissions/requesting
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
// Permission is not granted
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed; request the permission
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
} else {
// Permission has already been granted
}
如果您在 Android 10 上运行应用程序,则需要在 AndroidManifest.xml 文件中的应用程序标签android:requestLegacyExternalStorage="true"
添加此项。
这是一个临时解决方案。 必须使用 Android 11 Scoped Storage 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.