获取SD卡的读写权限

获取SD卡的读写权限

AndroidManifest.xml中写入这两行

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

声明该函数并在onCreate中调用即可。

private void requestMyPermissions() {
    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 100);
        Log.e(TAG,"无写SD权限");
    }
    else    Log.d(TAG,"有写SD权限");
    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.READ_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 100);
        Log.e(TAG,"无读SD权限");
    }
    else    Log.d(TAG,"有读SD权限");
}
posted @ 2020-04-25 20:57  caoanda  阅读(1320)  评论(0编辑  收藏  举报