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

时间:2020-01-22 12:14:13

标签: java android android-studio

我在android studio的外部存储中创建新目录时遇到问题。它可能必须要有权限才能执行某些操作,但是据我所知,一切似乎都很好。

我想做的是:

  1. 检查目录“ / storage / emulated / 0 / Recordings”是否存在,否则-创建目录
  2. 检查文件“ /storage/emulated/0/Recordings/tempFile.raw”是否存在,如果存在-删除它
  3. 使用“ 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中没有“没有工作”,表示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"/>

权限:

https://stackoverflow.com/image.jpg

虚拟设备上的外部存储空间:

https://stackoverflow.com/image.jpg

Build.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'
        }
    }
}

2 个答案:

答案 0 :(得分:2)

首先,您错过另一个权限:

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
}

答案 1 :(得分:2)

如果您在Android 10上运行应用程序,则需要在应用程序标签android:requestLegacyExternalStorage="true"中的AndroidManifest.xml文件中添加此代码。

这是一个临时解决方案。从Android 11开始,Scoped Storage必须使用。