Android允许从SD卡读取文件的权限

时间:2018-09-25 23:01:27

标签: java android memory runtime-permissions

嗨,我尝试从Android应用中从SD卡读取文件。在我的AndroidManiest.xml文件中,我已经指定了对外部存储设备的读写权限

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

这是我的代码,用于检查权限并尝试读取MainActivity.java中的文件。但是我无法读取我的文件。我做错了什么 ?请帮忙!

 int ReadExternalStoragePermission = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
        Log.e(TAG, "checkExternalStoragePermission() done");
        if(ReadExternalStoragePermission== PackageManager.PERMISSION_GRANTED) {
            Log.e(TAG, "ReadExternalStoragePermission() granted");

            //read my file in  /sdcard/test.csv

        }
        else {

        }

2 个答案:

答案 0 :(得分:0)

您首先需要请求用户许可:

public static final int PERMISSION_EXTERNAL_STORAGE = 1;
ActivityCompat.requestPermissions(thisActivity,
            new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
            PERMISSION_EXTERNAL_STORAGE);

答案 1 :(得分:0)

这是具有请求基本原理的请求权限实现。希望对别人有帮助。

public class MainActivity extends AppCompatActivity {

private View mainLayout;

private static final int PERMISSION_REQUEST_READ_EXTERNAL_STORAGE = 0; //Arbitrary >= 0


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

    mainLayout = findViewById(R.id.mainLayout); //SnackBar container view in your activity.

    //Runtime permission request required if Android permission >= Marshmallow
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
        checkPermission();
    else
        //read my file in  /sdcard/test.csv
        readTheFiles();
}

private void readTheFiles() {
    //This is the method where you read your SD Card content.
}


private void checkPermission() {
    // Check if the permission has been granted
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
            == PackageManager.PERMISSION_GRANTED) {
        // Permission is already available

        readTheFiles();
    } else {
        // Permission is missing and must be requested.
        requestReadExternalStoragePermission();
    }
}

private void requestReadExternalStoragePermission() {
    // Permission has not been granted and must be requested.
    if (ActivityCompat.shouldShowRequestPermissionRationale(this,
            Manifest.permission.READ_EXTERNAL_STORAGE)) {
        // Show a snackBar to the user that explain why you need this permission.
        Snackbar.make(mainLayout, "read_external_storage_permission_required",
                Snackbar.LENGTH_INDEFINITE).setAction(R.string.ok, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Request the permission
                ActivityCompat.requestPermissions(MainActivity.this,
                        new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                        PERMISSION_REQUEST_READ_EXTERNAL_STORAGE);
            }
        }).show();

    } else {
        // Request the permission.
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, PERMISSION_REQUEST_READ_EXTERNAL_STORAGE);
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                       @NonNull int[] grantResults) {
    if (requestCode == PERMISSION_REQUEST_READ_EXTERNAL_STORAGE) {
        // Request for permission.
        if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            // Permission has been granted.

            readTheFiles();
        } else {
            // Permission request was denied by user
            // Show a snackBar, exit program, close activity, etc.
        }
    }
}

}