猿问

`MediaStore.MediaColumns.DATA` 已弃用,我应该使用什么列作为替代

我想使用MediaStoreAPI 将媒体提供商的所有图片加载到我的应用程序,但此列MediaStore.MediaColumns.DATA已弃用。那么,我可以使用什么来替代呢?


我想更新以下代码以删除已弃用的MediaStore.MediaColumns.DATA列:


fun ContentResolver.loadImagesPaths(): List<String> {

    val uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI

    val images = mutableListOf<String>()

    val projection = arrayOf(MediaStore.MediaColumns.DATA)

    query(uri, projection, null, null, null)?.use { cursor ->

        val dataColumn = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA)

        while (cursor.moveToNext()) {

            images += cursor.getString(dataColumn)

        }

    }

    return images

}

编辑:


更新代码:


fun ContentResolver.loadImagesUris(): List<Uri> {

    val uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI

    val images = mutableListOf<Uri>()

    val projection = arrayOf(MediaStore.Images.Media._ID)

    query(uri, projection, null, null, null)?.use { cursor ->

        val idColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID)

        while (cursor.moveToNext()) {

            images += ContentUris.withAppendedId(uri, cursor.getLong(idColumn))

        }

    }

    return images

}

查看此链接,了解有关如何从共享存储访问媒体文件的更多详细信息:https://developer.android.com/training/data-storage/shared/media


幕布斯7119047
浏览 364回答 5
5回答

慕尼黑8549860

我能够将 MediaStore.MediaColumns.Data 替换为它自己的文件 ID(令人难以置信的是,文件具有 ID)并正确构建其 URI,如下所示:fun getAllShownImagesPath(activity: Activity): MutableList<Uri> {&nbsp; &nbsp; val uriExternal: Uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI&nbsp; &nbsp; val cursor: Cursor?&nbsp; &nbsp; val columnIndexID: Int&nbsp; &nbsp; val listOfAllImages: MutableList<Uri> = mutableListOf()&nbsp; &nbsp; val projection = arrayOf(MediaStore.Images.Media._ID)&nbsp; &nbsp; var imageId: Long&nbsp; &nbsp; cursor = activity.contentResolver.query(uriExternal, projection, null, null, null)&nbsp; &nbsp; if (cursor != null) {&nbsp; &nbsp; &nbsp; &nbsp; columnIndexID = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID)&nbsp; &nbsp; &nbsp; &nbsp; while (cursor.moveToNext()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; imageId = cursor.getLong(columnIndexID)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; val uriImage = Uri.withAppendedPath(uriExternal, "" + imageId)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; listOfAllImages.add(uriImage)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; cursor.close()&nbsp; &nbsp; }&nbsp; &nbsp; return listOfAllImages}然后使用 Uri 在您的视图中构建它!

喵喵时光机

仍然无法使用获得的 Uri 加载图像。 文档建议使用 openFileDescriptor() ,我这样做了,然后从中解码图像的位图:override fun loadImagesFromStorage(): List<AdapterImage> {    val uri: Uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI    val cursor: Cursor?    val columnIndexId: Int    val listOfAllImages = mutableListOf<AdapterImage>()    val projection = arrayOf(MediaStore.Images.Media._ID)    cursor = context.contentResolver        .query( uri, projection, null, null, null)    if ( cursor != null ){        columnIndexId = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID)        while (cursor.moveToNext()){            val contentUri = ContentUris.withAppendedId(uri, cursor.getLong(columnIndexId))            //here I open FileDescriptor and then decode it into Bitmap            var image: Bitmap            context.contentResolver.openFileDescriptor(contentUri, "r").use { pfd ->                if( pfd != null ){                    image = BitmapFactory.decodeFileDescriptor(pfd.fileDescriptor)                    listOfAllImages.add(AdapterImage(image))                }            }        }        cursor.close()    }    return listOfAllImages}PS 我的方法将返回我稍后在应用程序中使用的 AdapterImage 对象列表,但此时您可以将任何需要的东西放在那里

PIPIONE

我终于通过创建这个类解决了这个问题class FileHelper {val mediaType = "multipart/form-data".toMediaTypeOrNull()fun getPartBodyFromUri(context: Context, uri: Uri): MultipartBody.Part {&nbsp; &nbsp; val realPath = getPathFromURI(context, uri)&nbsp; &nbsp; val fileImage = createFile(realPath)&nbsp; &nbsp; val requestBody = createRequestBody(fileImage)&nbsp; &nbsp; return createPart(fileImage, requestBody)}private fun createFile(realPath: String): File {&nbsp; &nbsp; return File(realPath)}private fun createRequestBody(file: File): RequestBody {&nbsp; &nbsp; return file.asRequestBody(mediaType)}private fun createPart(file: File, requestBody: RequestBody): MultipartBody.Part {&nbsp; &nbsp; return MultipartBody.Part.createFormData("imageFile", file.name, requestBody)}private fun getPathFromURI(context: Context, uri: Uri): String {&nbsp; &nbsp; var realPath = String()&nbsp; &nbsp; uri.path?.let { path ->&nbsp; &nbsp; &nbsp; &nbsp; val databaseUri: Uri&nbsp; &nbsp; &nbsp; &nbsp; val selection: String?&nbsp; &nbsp; &nbsp; &nbsp; val selectionArgs: Array<String>?&nbsp; &nbsp; &nbsp; &nbsp; if (path.contains("/document/image:")) { // files selected from "Documents"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; databaseUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; selection = "_id=?"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; selectionArgs = arrayOf(DocumentsContract.getDocumentId(uri).split(":")[1])&nbsp; &nbsp; &nbsp; &nbsp; } else { // files selected from all other sources, especially on Samsung devices&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; databaseUri = uri&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; selection = null&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; selectionArgs = null&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; val column = "_data"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; val projection = arrayOf(column)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; val cursor = context.contentResolver.query(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; databaseUri,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; projection,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; selection,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; selectionArgs,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; null&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cursor?.let {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (it.moveToFirst()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; val columnIndex = cursor.getColumnIndexOrThrow(column)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; realPath = cursor.getString(columnIndex)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cursor.close()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } catch (e: Exception) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; println(e)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return realPath}}Media.DATA 已弃用,使用“MediaStore.Images.Media._ID”来获取正确的列,但无法正常工作,因此我创建了我需要的列val column = "_data"val projection = arrayOf(column)然后我使用 getColumnIndexOrThrow() 方法来获取正确的索引val columnIndex = cursor.getColumnIndexOrThrow(column)realPath = cursor.getString(columnIndex)

繁星点点滴滴

在爪哇FileInputStream input = null;&nbsp; &nbsp; FileOutputStream output = null;&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; String filePath = new File(getCacheDir(), "tmp").getAbsolutePath();&nbsp; &nbsp; &nbsp; &nbsp; android.os.ParcelFileDescriptor pfd = getContentResolver ().openFileDescriptor(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sharedFileUri, "r");&nbsp; &nbsp; &nbsp; &nbsp; if (pfd != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FileDescriptor fd = pfd . getFileDescriptor ();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; input = new FileInputStream (fd);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; output = new FileOutputStream (filePath);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int read;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; byte[] bytes = new byte[4096];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while ((read = input.read(bytes)) != -1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; output.write(bytes, 0, read);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; File sharedFile = new File(filePath);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String finalPath = sharedFile.getPath();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }catch(Exception ex) {&nbsp; &nbsp; } finally {&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; input.close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; output.close();&nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception ignored) {&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }

凤凰求蛊

从内部存储加载图像。使用 MediaStore 类读取包含图像和视频的所有文件夹。并将结果作为 ArrayList 返回。private fun getAllShownImagesPath(activity: Activity): ArrayList<Albums> {val uri: Urival cursor: Cursorvar cursorBucket: Cursorval column_index_data: Intval column_index_folder_name: Intval listOfAllImages = ArrayList<String>()var absolutePathOfImage: String? = nullvar albumsList = ArrayList<Albums>()var album: Albums? = nullval BUCKET_GROUP_BY = "1) GROUP BY 1,(2"val BUCKET_ORDER_BY = "MAX(datetaken) DESC"uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URIval projection = arrayOf(MediaStore.Images.ImageColumns.BUCKET_ID,&nbsp; &nbsp; &nbsp; &nbsp; MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME,&nbsp; &nbsp; &nbsp; &nbsp; MediaStore.Images.ImageColumns.DATE_TAKEN,&nbsp; &nbsp; &nbsp; &nbsp; MediaStore.Images.ImageColumns.DATA)cursor = activity.contentResolver.query(uri, projection, BUCKET_GROUP_BY, null, BUCKET_ORDER_BY)if (cursor != null) {&nbsp; &nbsp; column_index_data = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA)&nbsp; &nbsp; column_index_folder_name = cursor&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME)&nbsp; &nbsp; while (cursor.moveToNext()) {&nbsp; &nbsp; &nbsp; &nbsp; absolutePathOfImage = cursor.getString(column_index_data)&nbsp; &nbsp; &nbsp; &nbsp; Log.d("title_apps", "bucket name:" + cursor.getString(column_index_data))&nbsp; &nbsp; &nbsp; &nbsp; val selectionArgs = arrayOf("%" + cursor.getString(column_index_folder_name) + "%")&nbsp; &nbsp; &nbsp; &nbsp; val selection = MediaStore.Images.Media.DATA + " like ? "&nbsp; &nbsp; &nbsp; &nbsp; val projectionOnlyBucket = arrayOf(MediaStore.MediaColumns.DATA, MediaStore.Images.Media.BUCKET_DISPLAY_NAME)&nbsp; &nbsp; &nbsp; &nbsp; cursorBucket = activity.contentResolver.query(uri, projectionOnlyBucket, selection, selectionArgs, null)&nbsp; &nbsp; &nbsp; &nbsp; Log.d("title_apps", "bucket size:" + cursorBucket.count)&nbsp; &nbsp; &nbsp; &nbsp; if (absolutePathOfImage != "" && absolutePathOfImage != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; listOfAllImages.add(absolutePathOfImage)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; albumsList.add(Albums(cursor.getString(column_index_folder_name), absolutePathOfImage, cursorBucket.count, false))&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}return getListOfVideoFolders(albumsList)}// 该函数负责读取所有文件夹中的所有视频。private fun getListOfVideoFolders(albumsList: ArrayList<Albums>): ArrayList<Albums> {var cursor: Cursorvar cursorBucket: Cursorvar uri: Urival BUCKET_GROUP_BY = "1) GROUP BY 1,(2"val BUCKET_ORDER_BY = "MAX(datetaken) DESC"val column_index_album_name: Intval column_index_album_video: Inturi = android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URIval projection1 = arrayOf(MediaStore.Video.VideoColumns.BUCKET_ID,&nbsp; &nbsp; &nbsp; &nbsp; MediaStore.Video.VideoColumns.BUCKET_DISPLAY_NAME,&nbsp; &nbsp; &nbsp; &nbsp; MediaStore.Video.VideoColumns.DATE_TAKEN,&nbsp; &nbsp; &nbsp; &nbsp; MediaStore.Video.VideoColumns.DATA)cursor = this.contentResolver.query(uri, projection1, BUCKET_GROUP_BY, null, BUCKET_ORDER_BY)if (cursor != null) {&nbsp; &nbsp; column_index_album_name = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.BUCKET_DISPLAY_NAME)&nbsp; &nbsp; column_index_album_video = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA)&nbsp; &nbsp; while (cursor.moveToNext()) {&nbsp; &nbsp; &nbsp; &nbsp; Log.d("title_apps", "bucket video:" + cursor.getString(column_index_album_name))&nbsp; &nbsp; &nbsp; &nbsp; Log.d("title_apps", "bucket video:" + cursor.getString(column_index_album_video))&nbsp; &nbsp; &nbsp; &nbsp; val selectionArgs = arrayOf("%" + cursor.getString(column_index_album_name) + "%")&nbsp; &nbsp; &nbsp; &nbsp; val selection = MediaStore.Video.Media.DATA + " like ? "&nbsp; &nbsp; &nbsp; &nbsp; val projectionOnlyBucket = arrayOf(MediaStore.MediaColumns.DATA, MediaStore.Video.Media.BUCKET_DISPLAY_NAME)&nbsp; &nbsp; &nbsp; &nbsp; cursorBucket = this.contentResolver.query(uri, projectionOnlyBucket, selection, selectionArgs, null)&nbsp; &nbsp; &nbsp; &nbsp; Log.d("title_apps", "bucket size:" + cursorBucket.count)&nbsp; &nbsp; &nbsp; &nbsp; albumsList.add(Albums(cursor.getString(column_index_album_name), cursor.getString(column_index_album_video), cursorBucket.count, true))&nbsp; &nbsp; }}return albumsList}
随时随地看视频慕课网APP

相关分类

Java
我要回答