MediaStore.MediaColumns.DATA已过时,我想将图像从图库加载到我的应用程序 [英] MediaStore.MediaColumns.DATA is deprecated, and I want to load images from gallery to my app

查看:2539
本文介绍了MediaStore.MediaColumns.DATA已过时,我想将图像从图库加载到我的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用MediaStore.MediaColumns.DATA将厨房中的所有图片加载到我的应用程序中,但已弃用.那么,加载它们的另一种方法是什么?

I want to load all the pictures from the galley to my app by using MediaStore.MediaColumns.DATA , but it is deprecated. So, what is the other way to load them?

我现在使用此代码,但是我已经说过了:

I use this code now, but it is deprecated as I said:

fun getAllShownImagesPath(activity: Activity): MutableList<String> {
    val uri: Uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
    val cursor: Cursor?
    val columnIndexData: Int
    val listOfAllImages: MutableList<String> = mutableListOf()
    val projection = arrayOf(MediaStore.MediaColumns.DATA)
    var absolutePathOfImage: String
    cursor = activity.contentResolver.query(uri, projection, null, null, null)
    if (cursor != null) {
        columnIndexData = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA)
        while (cursor.moveToNext()) {
            absolutePathOfImage = cursor.getString(columnIndexData)
            listOfAllImages.add(absolutePathOfImage)
        }
        cursor.close()
    }
    return listOfAllImages
}

推荐答案

我能够用其自己的文件ID(很可能是文件具有ID)替换MediaStore.MediaColumns.Data并正确地构造其URI,如下所示:

I was able to replace MediaStore.MediaColumns.Data with its own file ID (incredibly, files have IDs) and correctly constructing its URI, like this:

fun getAllShownImagesPath(activity: Activity): MutableList<Uri> {
    val uriExternal: Uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
    val cursor: Cursor?
    val columnIndexID: Int
    val listOfAllImages: MutableList<Uri> = mutableListOf()
    val projection = arrayOf(MediaStore.Images.Media._ID)
    var imageId: Long
    cursor = activity.contentResolver.query(uriExternal, projection, null, null, null)
    if (cursor != null) {
        columnIndexID = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID)
        while (cursor.moveToNext()) {
            imageId = cursor.getLong(columnIndexID)
            val uriImage = Uri.withAppendedPath(uriExternal, "" + imageId)
            listOfAllImages.add(uriImage)
        }
        cursor.close()
    }
    return listOfAllImages
}

,然后使用Uri在视图中构建它!

这篇关于MediaStore.MediaColumns.DATA已过时,我想将图像从图库加载到我的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆