往指定目录写入图片
fun save(view: View) {
val bitmap = BitmapFactory.decodeResource(resources, R.drawable.meta_image_home_bg)
val contentValues = contentValuesOf(
MediaStore.MediaColumns.DISPLAY_NAME to System.currentTimeMillis().toString(),
MediaStore.MediaColumns.MIME_TYPE to "image/jpeg",
MediaStore.MediaColumns.RELATIVE_PATH to "Pictures/马车保元界/"
)
contentResolver.insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
contentValues
)?.apply {
contentResolver.openOutputStream(this)
.use {
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, it)
}
}
}
读取指定目录的图片
private fun loadTargetFilePhotos(name: String) {
val result = contentResolver.query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
arrayOf(
MediaStore.Images.Media.DATA,
MediaStore.Images.Media._ID,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME
),
MediaStore.Images.Media.BUCKET_DISPLAY_NAME + " = ?",
arrayOf(name),
null
)
result?.apply {
val count = count
if (moveToFirst()) {
val dataColumn = getColumnIndex(MediaStore.Images.Media.DATA)
val bucketColumn = getColumnIndex(MediaStore.Images.Media.BUCKET_DISPLAY_NAME)
do {
val path = this.getString(dataColumn)
val bucket = this.getString(bucketColumn)
val uri = Uri.parse(path)
Log.d("loadTargetFilePhotos", path)
} while (moveToNext())
}
close()
}
}
网友评论