我想通过单击按钮从图库中选择多个媒体(视频或图像), 但只有在仅在我的应用程序的活动中显示图库中的所有媒体之后, (无需打开我的手机默认图库)然后,在回收器视图(网格视图)中显示它们。
然后,从中选择视频图像。我是安卓初学者, 也没有从其他资源找到任何帮助。请同样指导我并提供适当的解决方案。
嗨,我可以理解你的问题,你想要的是一个图像/视频选择器库。作为初学者自己构建它是个坏主意,所以我的建议是选择社区构建库,这将使你的工作变得更轻松
一些图像选择器库的链接 https://android-arsenal.com/tag/157你可以谷歌找到更多的库
按照他们的 Github 页面的说明并尝试在你的项目中实现它们,这会容易得多
我的建议是使用这个库https://github.com/Mindinventory/Lassi-Android 因为这个库提供了图像和视频可供选择
您可以获得按时间排序的图像和视频列表,然后可以在适配器中显示。
第 1 步:-声明变量
var image: ArrayList<FileMediaDetails>? = null
var video: ArrayList<FileMediaDetails>? = null
第 2 步:- 创建负责设置的函数 openDialog 带 recyclerview 的适配器
private fun openDialog() {
val view: View = layoutInflater.inflate(R.layout.dialog_gallery, null)
val dialog = BottomSheetDialog(this, R.style.AppBottomSheetDialogTheme)
dialog.setContentView(view)
image = getData()
video = getVideoList()
val newSet: MutableSet<FileMediaDetails> = HashSet<FileMediaDetails>(image)
newSet.addAll(video!!)
val list = newSet.toList()
var newList: List<FileMediaDetails> = ArrayList(newSet)
newList = list.sortedWith(compareBy { it.dateModified })
newList = newList.reversed()
// Collections.shuffle(newList)
dialog.rvImages.layoutManager = GridLayoutManager(this, 3, RecyclerView.VERTICAL, false)
dialog.rvImages.adapter = ImagesAdapter(this, newList)
dialog.rvImages.addOnItemTouchListener(
RecyclerTouchListener(this, dialog.rvImages, RecyclerTouchListener.ClickListener { _, position ->
myFile = File(newList[position].path)
uri = Uri.parse(newList[position].path)
val extension = getMimeType(this, uri!!)
if (extension == "mp4") {
if (isFileLessThan100MB(myFile!!)) // sending video
askDoubtWithUrl("video")
else
Utils.showToast(this, "Your file is too big to upload", AppConstant.FAILED)
} else // sending image
askDoubtWithUrl("image")
dialog.dismiss()
})
)
dialog.show()
}
第3步:- getData用于获取图像列表
@SuppressLint("Range")
private fun getData(): ArrayList<FileMediaDetails> {
// val arrayList: ArrayList<String> = ArrayList()
val arrayList: ArrayList<FileMediaDetails> = ArrayList()
val projection = arrayOf(MediaStore.MediaColumns.DATA,MediaStore.MediaColumns.DATE_MODIFIED)
val cursor = contentResolver.query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null,
null,
null
)
while (cursor!!.moveToNext()) {
val absolutePathOfImage = cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DATA))
val createdTime = cursor.getLong(cursor.getColumnIndex(MediaStore.MediaColumns.DATE_MODIFIED))
arrayList.add(FileMediaDetails(absolutePathOfImage, createdTime))
}
cursor.close()
return arrayList
}
第 4 步:- getVideoList 用于获取视频列表
private fun getVideoList(): ArrayList<FileMediaDetails> {
val uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI
val projection = arrayOf(MediaStore.Video.VideoColumns.DATA, MediaStore.MediaColumns.DATE_MODIFIED)
val cursor: Cursor? = contentResolver.query(uri, projection, null, null, null)
val pathArrList: ArrayList<FileMediaDetails> = ArrayList()
if (cursor != null) {
while (cursor.moveToNext()) {
val absolutePathOfImage = cursor.getString(0)
val createdTime = cursor.getLong(cursor.getColumnIndex(MediaStore.MediaColumns.DATE_MODIFIED))
pathArrList.add(FileMediaDetails(absolutePathOfImage,createdTime))
}
cursor.close()
}
return pathArrList
}
请参阅 Adam Sinicki 的 https://www.androidauthority.com/how-to-build-an-image-gallery-app-718976/ 和 https://codingwithmitch.com/blog/playing-video- recyclerview-exoplayer-android 作者:Mitch Tabian