簡體   English   中英

打開失敗的EACCES(權限被拒絕)

[英]Getting Open Failed EACCES(Permission Denied)

我想將數據寫入SD卡,但出現以下錯誤:無法打開失敗:EACCES(權限被拒絕)。 我正在使用的Android版本是jelly bean(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>

您不能假設外部存儲將始終位於“ / mnt / extsd”。 這種情況很少發生,因為安裝點取決於OEM。 使用Context對象中的標准API來獲取感興趣的正確位置: Context.getExternalFilesDir()Context.getExternalFilesDirs()

問題出在果凍豆

這項工作對我來說

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";
         }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM