Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save yudikarma/b617ed0511817ea39b6c3f63abda4f49 to your computer and use it in GitHub Desktop.
Save yudikarma/b617ed0511817ea39b6c3f63abda4f49 to your computer and use it in GitHub Desktop.
fun getImagelist(): ArrayList<GaleryFragmentModel> {
var dataLocal = ArrayList<GaleryFragmentModel>()
var absolutePathOfFile: String? = null
val projection = arrayOf(
MediaStore.Files.FileColumns.DATA,
MediaStore.Files.FileColumns.DATE_ADDED,
MediaStore.Files.FileColumns.MEDIA_TYPE)
val selection:String = (MediaStore.Files.FileColumns.MEDIA_TYPE + "="
+ MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE
+ " OR "
+ MediaStore.Files.FileColumns.MEDIA_TYPE + "="
+ MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO)
val queryUri = MediaStore.Files.getContentUri("external")
val cursorLoader = CursorLoader(context,queryUri,projection,selection,null,MediaStore.Files.FileColumns.DATE_ADDED + " DESC")
val cursor = cursorLoader.loadInBackground()
cursor?.let {
while (it.moveToNext()){
val dataColumnIndex = it.getColumnIndex(MediaStore.Files.FileColumns.DATA)
val typeMedia = it.getColumnIndex(MediaStore.Files.FileColumns.MEDIA_TYPE)
absolutePathOfFile = it.getString(dataColumnIndex)
val mediaType = it.getInt(typeMedia)/*1 == image , 3 == video*/
absolutePathOfFile?.let { pathfile ->
galeryFragmentModel = GaleryFragmentModel(pathfile,mediaType)
galeryFragmentModel?.let {
dataLocal.add(it)
}
}
}
}
cursor?.close()
return dataLocal
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment