确保设备是否有外部存储
fun judgeCard() {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
}
}
android6.0以上需要动态申请权限
fun requestPermission() {
val permission = ContextCompat.checkSelfPermission(
this,
Manifest.permission.WRITE_EXTERNAL_STORAGE
)
if (permission != PackageManager.PERMISSION_GRANTED) {
//动态申请权限
ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
1
)
}
}
//当动态申请权限的过程中 requestCode等于几 做什么操作
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == 1) {
}
}
Environment.getExternalStorageDirectory() 获取的目录才是sdcard的实际目录(系统不同会造成路径有差异)