获取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权限");
}

CAD加油!欢迎跟我一起讨论学习算法