gpt4 book ai didi

java - 即使授予权限,也无法在外部存储中创建目录

转载 作者:行者123 更新时间:2023-12-05 00:20:22 29 4
gpt4 key购买 nike

我在 android studio 的外部存储上创建新目录时遇到问题。它可能需要做一些有权限的事情,但据我所知,一切似乎都很好。

我想做的是:

  1. Check if directory "/storage/emulated/0/Recordings" exists, if not - create the directory
  2. Check if file "/storage/emulated/0/Recordings/tempFile.raw" exists, if exists - delete it
  3. Create a file inside the directory using "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)

Notice: There isn't "Didn't work" in logcat, meaning file.mkdirs() was executed.



list 权限:
<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"/>

权限:

https://stackoverflow.com/image.jpg

虚拟设备上的外部存储:

https://stackoverflow.com/image.jpg

构建.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



此外,您必须明确要求您的 Activity 获得许可。检查此链接:

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
}

关于java - 即使授予权限,也无法在外部存储中创建目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59859556/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com