我尝试获取图像的文件路径,并且有效:
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对象上传到服务器上。