获取SD卡的读写权限
原创
©著作权归作者所有:来自51CTO博客作者caoanda的原创作品,请联系作者获取转载授权,否则将追究法律责任
获取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加油!欢迎跟我一起讨论学习算法