如何获取从存储中选取的图像的真实路径-Android API 29?

时间:2020-04-26 19:51:44

标签: android kotlin android-cursor android-storage

我尝试获取图像的文件路径,并且有效:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_CODE){
            val image = data!!.data
            val projection: Array<out String> = arrayOf(MediaStore.Images.Media.DATA)
            val cursor = context!!.contentResolver.query(selectedImage, projection, null, null, null)
            var filepath = ""
            if(cursor!!.moveToFirst()){
                val columnIndex = cursor.getColumnIndex(projection[0])
                filepath = cursor.getString(columnIndex)
            }

但不建议使用MediaStore.Images.Media.DATA。我应该如何以正确的方式获取文件路径?我需要使File对象上传到服务器上。

0 个答案:

没有答案