简体   繁体   English

Android:在外部存储中创建新文件时,权限被拒绝

[英]Android: Permission denied when creating a new file in external storage

I am trying to write a file to the external storage location on Android. 我正在尝试将文件写入Android上的外部存储位置。 Here is my code : 这是我的代码:

String state = Environment.getExternalStorageState();

if(Environment.MEDIA_MOUNTED.equals(state)) {
     saveState= true;
     File pPath= Environment.getExternalStorageDirectory();

     if(!pPath.exists()) {
          boolean bReturn= pPath.mkdirs();
          Log.d(TAG, "mkdirs returned: " + bReturn);
     }
     try {
          File pFile= new File(pPath, "output.pcm");
          pFile.createNewFile();
          outputLocation= pFile.getAbsolutePath().toString();
     } catch (IOException e) {
          Log.d(TAG, "Could not create file: " + e.toString());
          saveState= false;
     }
} // end if we can read/write to the external storage

The call to createNewFile() returns "Could not create file: java.io.IOException: Permission denied" 对createNewFile()的调用返回“无法创建文件:java.io.IOException:权限被拒绝”

I have the android.permission.WRITE_EXTERNAL_STORAGE in my manifest file. 我的清单文件中有android.permission.WRITE_EXTERNAL_STORAGE。 There seems to be a ton of questions about this problem, and this solved most of them, but I am still having this issue. 关于这个问题似乎有很多问题,这解决了大多数问题,但是我仍然遇到这个问题。 Here is part of the manifest: 这是清单的一部分:

<?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="company.example.sample"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />
    <permission android:name="android.permission.RECORD_AUDIO"></permission>
    <permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"></permission>
    <permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></permission>

Any ideas? 有任何想法吗?

Replace your <permission> elements with <uses-permission> elements: <permission>元素替换为<uses-permission>元素:

<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<permission> defines a new permission; <permission>定义一个新的权限; <uses-permission> indicates you want to hold a permission. <uses-permission>表示您要持有权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM