0

I try to get filepath of image and it works:

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)
            }

But MediaStore.Images.Media.DATA is deprecated. How should I get filepath in proper way? I need to make File object to upload it on server.

  • "I need to make File object to upload it on server" -- you do not need a `File` object to upload the content identified by a `Uri` to a server. See [this](https://stackoverflow.com/q/56308559/115145), for example. "But MediaStore.Images.Media.DATA is deprecated" -- beyond that, it does not work reliably. – CommonsWare Apr 26 '20 at 20:21
  • But is there any way to get file path? – invokeLater Apr 26 '20 at 21:27
  • It will not do you any good. If the media is located on removable storage, for example, you will not have filesystem access to it, on Android 4.4 or higher. – CommonsWare Apr 26 '20 at 21:38
  • Is there any way to get image and create File object with it? – invokeLater Apr 26 '20 at 21:50

0 Answers0