获取开放失败的EACCES(权限被拒绝)

时间:2014-12-10 11:36:37

标签: java android storage android-sdcard

我想将数据写入SD卡,但我收到以下错误:失败打开失败:EACCES(权限被拒绝)。 我正在研究的Android版本是果冻豆(4.3)。 我还在清单文件中给予了许可。

这是我的代码:

 package com.example.androidsdcard;

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;

    import android.app.Activity;
    import android.os.Bundle;
    import android.os.Environment;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;

    public class MainActivity extends Activity {
        // GUI controls
        EditText txtData;
        Button btnWriteSDFile;
        Button btnReadSDFile;
        Button btnClearScreen;
        Button btnClose;
        File sample=null;
        String SDCard_Path="/mnt/extsd";
        @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // bind GUI elements with local controls
        txtData = (EditText) findViewById(R.id.txtData);
        txtData.setHint("Enter some lines of data here...");

        btnWriteSDFile = (Button) findViewById(R.id.btnWriteSDFile);
        btnWriteSDFile.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // write on SD card file data in the text box

            File storageDir = new File(SDCard_Path);
            String sample1 = Environment.getExternalStorageDirectory().getPath();
            Toast.makeText(MainActivity.this,sample1, Toast.LENGTH_LONG).show();
            if(sample == null)
            {
                Toast.makeText(MainActivity.this,"Sample == null executed", Toast.LENGTH_LONG).show();

            }
            else

                Toast.makeText(MainActivity.this,"Sample == null skipped", Toast.LENGTH_LONG).show();

            if(storageDir.isDirectory())
            {
                String[] dirList = storageDir.list();
                if(dirList==null)
                {
                    Toast.makeText(getBaseContext(),"Failed to detect SD card",Toast.LENGTH_SHORT).show();
                    return;
                }
                else
                Toast.makeText(getBaseContext(),"SD Card Detected",Toast.LENGTH_SHORT).show();  
            }
            try {
                File myFile = new File("/mnt/extsd/MedeQuip.txt");
            /*  if(myFile.createNewFile()==false)
                {
                    Toast.makeText(getBaseContext(),"Unable to create File'",Toast.LENGTH_SHORT).show();    
                    return;
                }
                else
                    Toast.makeText(getBaseContext(),"File Created'",Toast.LENGTH_SHORT).show(); 
            */  FileOutputStream fOut = new FileOutputStream(myFile);
                OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
                myOutWriter.append(txtData.getText());
                myOutWriter.close();
                fOut.close();
                Toast.makeText(getBaseContext(),"Done writing SD mysdfile.txt'",Toast.LENGTH_SHORT).show();
            } catch (Exception e) 
            {
                Toast.makeText(getBaseContext(), e.getMessage(),Toast.LENGTH_SHORT).show();
            }
        }// onClick
        }); // btnWriteSDFile

            btnReadSDFile = (Button) findViewById(R.id.btnReadSDFile);
            btnReadSDFile.setOnClickListener(new OnClickListener() 
            {

            public void onClick(View v) 
            {
                // write on SD card file data in the text box
            try
            {
                File myFile = new File(SDCard_Path+"/MedeQuip.txt");
                FileInputStream fIn = new FileInputStream(myFile);
                BufferedReader myReader = new BufferedReader(
                        new InputStreamReader(fIn));
                String aDataRow = "";
                String aBuffer = "";
                while ((aDataRow = myReader.readLine()) != null) {
                    aBuffer += aDataRow + "\n";
                }
                txtData.setText(aBuffer);
                myReader.close();
                Toast.makeText(getBaseContext(),"Done reading SD 'MedeQuip.txt'",Toast.LENGTH_SHORT).show();
            } 
            catch (Exception e) 
            {
                Toast.makeText(getBaseContext(), e.getMessage(),Toast.LENGTH_SHORT).show();
            }
            }// onClick
            }); // btnReadSDFile

            btnClearScreen = (Button) findViewById(R.id.btnClearScreen);
            btnClearScreen.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    // clear text box
                    txtData.setText("");
                }
            }); // btnClearScreen

            btnClose = (Button) findViewById(R.id.btnClose);
            btnClose.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    // clear text box
                    finish();
                }
            }); // btnClose

        }// onCreate

    }// AndSDcard

这是我的xml:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <EditText
    android:id="@+id/txtData"
    android:layout_width="fill_parent"
    android:layout_height="180px"
    android:textSize="18sp"  />

    <Button
        android:id="@+id/btnWriteSDFile"
        android:layout_width="143px"
        android:layout_height="44px"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/txtData"
        android:layout_marginTop="60dp"
        android:text="1. Write SD File" />

    <Button
        android:id="@+id/btnClearScreen"
        android:layout_width="141px"
        android:layout_height="42px"
        android:layout_alignBaseline="@+id/btnWriteSDFile"
        android:layout_alignBottom="@+id/btnWriteSDFile"
        android:layout_marginLeft="30dp"
        android:layout_toRightOf="@+id/btnWriteSDFile"
        android:text="2. Clear Screen" />

    <Button
        android:id="@+id/btnReadSDFile"
        android:layout_width="140px"
        android:layout_height="42px"
        android:layout_alignTop="@+id/btnWriteSDFile"
        android:layout_marginLeft="32dp"
        android:layout_toRightOf="@+id/btnClearScreen"
        android:text="3. Read SD File" />

    <Button
        android:id="@+id/btnClose"
        android:layout_width="141px"
        android:layout_height="43px"
        android:layout_alignTop="@+id/btnClearScreen"
        android:layout_marginLeft="61dp"
        android:layout_toRightOf="@+id/btnReadSDFile"
        android:text="4. Close" />

    </RelativeLayout>

这是我的清单文件:

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
</manifest>

2 个答案:

答案 0 :(得分:1)

您不能假设外部存储始终位于“/ mnt / extsd”。很少会出现这种情况,因为它取决于安装点所在的OEM。使用Context对象中的标准API来获取正确的感兴趣位置:Context.getExternalFilesDir()Context.getExternalFilesDirs()

答案 1 :(得分:0)

问题出在果冻豆上

这项工作对我来说

String path = null;
      if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT){
          path = getActivity().getCacheDir() + File.separator + "name";
         } else{
          path = Environment.getExternalStorageDirectory() + File.separator + "name";
         }