MediaStore.MediaColumns.DATA is deprecated, and I want to load images from gallery to my app

前端 未结 4 1693
遥遥无期
遥遥无期 2021-02-03 11:05

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

4条回答
  •  梦如初夏
    2021-02-03 11:26

    I finally solved the problem by creating this class

    class FileHelper {
    val mediaType = "multipart/form-data".toMediaTypeOrNull()
    
    fun getPartBodyFromUri(context: Context, uri: Uri): MultipartBody.Part {
        val realPath = getPathFromURI(context, uri)
        val fileImage = createFile(realPath)
        val requestBody = createRequestBody(fileImage)
        return createPart(fileImage, requestBody)
    }
    
    private fun createFile(realPath: String): File {
        return File(realPath)
    }
    
    private fun createRequestBody(file: File): RequestBody {
        return file.asRequestBody(mediaType)
    }
    
    private fun createPart(file: File, requestBody: RequestBody): MultipartBody.Part {
        return MultipartBody.Part.createFormData("imageFile", file.name, requestBody)
    }
    
    private fun getPathFromURI(context: Context, uri: Uri): String {
        var realPath = String()
        uri.path?.let { path ->
    
            val databaseUri: Uri
            val selection: String?
            val selectionArgs: Array?
            if (path.contains("/document/image:")) { // files selected from "Documents"
                databaseUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
                selection = "_id=?"
                selectionArgs = arrayOf(DocumentsContract.getDocumentId(uri).split(":")[1])
            } else { // files selected from all other sources, especially on Samsung devices
                databaseUri = uri
                selection = null
                selectionArgs = null
            }
            try {
                val column = "_data"
                val projection = arrayOf(column)
                val cursor = context.contentResolver.query(
                    databaseUri,
                    projection,
                    selection,
                    selectionArgs,
                    null
                )
                cursor?.let {
                    if (it.moveToFirst()) {
                        val columnIndex = cursor.getColumnIndexOrThrow(column)
                        realPath = cursor.getString(columnIndex)
                    }
                    cursor.close()
                }
            } catch (e: Exception) {
                println(e)
            }
        }
        return realPath
    }
    

    }

    Media.DATA it's deprecate and "MediaStore.Images.Media._ID" to get the correct column, not working so I create column I need

    val column = "_data"
    val projection = arrayOf(column)
    

    then I use getColumnIndexOrThrow() method to get correct index

    val columnIndex = cursor.getColumnIndexOrThrow(column)
    realPath = cursor.getString(columnIndex)
    

提交回复
热议问题