android - sd卡写入权限被拒绝(EACCES)

时间:2017-07-17 08:04:01

标签: android sd-card permission-denied

我试图写入SD卡,而不是写入"外部"存储在设备上的存储(调用getExternalStorageDirectory时返回的存储)。我可以读取SD卡中的每个文件,但无法在其PUBLIC目录中更改或写入任何内容,在尝试时检索此错误:" java.io.IOException:open failed:EACCES(Permission denied) "

我在这里看到了许多像这样的问题的答案,但大多数只讨论了外部存储" (主要),这不是SD卡(现在大多数设备上)。我知道我可以写入我的应用程序的SD卡私人目录,但这不是我想要的,我想改变公共文件。最后,有些人告诉我,我无法写入Android 6.0 Marshmallow的SD卡公共目录,但我不相信Android会以这种方式损害开发人员。

注意:我在运行时请求权限,但即使在运行时获得授权,直接在应用程序管理器中授权,设备已与USB连接或断开连接,错误仍然存​​在。

我使用的代码如下:

import android.Manifest;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import static android.widget.Toast.LENGTH_LONG;

public class MainActivity extends AppCompatActivity {

    private static String TAG = "SdCardTest";
    private static final int REQUEST_WRITE_STORAGE = 101;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        int permission = ContextCompat.checkSelfPermission(this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE);

        if (permission != PackageManager.PERMISSION_GRANTED) {
            Log.i(TAG, "Permission to record denied");

            if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setMessage("Permission is required to access SD card.").setTitle("Permission required");

                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int id) {
                        Log.i(TAG, "Clicked");
                        makeRequest();
                    }
                });

                AlertDialog dialog = builder.create();
                dialog.show();

            } else {
                makeRequest();
            }
        }
    }

    protected void makeRequest() {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_WRITE_STORAGE);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case REQUEST_WRITE_STORAGE: {

                if (grantResults.length == 0 || grantResults[0] != PackageManager.PERMISSION_GRANTED) {

                    Log.i(TAG, "Permission has been denied by user");
                    Toast.makeText(this, "Permission has been denied by user", LENGTH_LONG).show();

                } else {

                    Log.i(TAG, "Permission has been granted by user");
                    Toast.makeText(this, "Permission has been granted by user", LENGTH_LONG).show();

                    //Here I WRITE to SD card

                    } catch (IOException e) {
                        e.printStackTrace();
                        Toast.makeText(this, "Not working", LENGTH_LONG).show();
                    }

                }
                return;
            }
        }
    }
}

我的清单文件:

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

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

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

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

</manifest>

0 个答案:

没有答案