打开图库拿到图片
/**
* 相册
*/
private fun takeGallery() {
val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
intent.type = "image/*"
startActivityForResult(intent, REQUEST_CODE_PICK_PHOTO)
}
获取图片的路径
REQUEST_CODE_PICK_PHOTO -> {
//获取图片的Uri地址
val currentPictureUri = data?.data
currentPictureUri?.apply {
//从系统表中查询指定Uri对应的照片
val filePathColumn = arrayOf(MediaStore.Images.Media.DATA)
val cursor = contentResolver.query(currentPictureUri!!, filePathColumn, null, null, null)
cursor?.moveToFirst()
//获取照片路径
val path = cursor?.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA))
cursor?.close()
//把Uri转file
val currentPictureFile = File(path)
val picture = Picture()
picture.file = currentPictureFile
picture.uri = currentPictureUri
pictures.add(pictures.size - 1, picture)
pictureAdapter.notifyDataSetChanged()
tv_picture_num.text = "(${pictures.size - 1}/5)"
}
}