0

I am trying to upload a file from my device to my android app using the below code.

Intent(Intent.ACTION_OPEN_DOCUMENT).apply {

                type = "*/*"

                putExtra(Intent.EXTRA_MIME_TYPES, arrayOf(

                    "image/*",

                    "application/pdf", // .pdf

                    "application/vnd.oasis.opendocument.text"

                ))

            },

            1

        )

    }

OnActivityResult code:

if (requestCode == 1) {

        val fileName = findViewById<TextView>(R.id.fileName)

        val file = File(data?.data?.path)



    Log.d("TAG", "Uri:: ${data?.data.toString()} ${file.absoluteFile}")

}

When using Emulator to test the upload , the file name is correctly reflected but when testing through a device (Android 8 and above) , the file name and file Uri are not correct.

**The URI: content://com.android.providers.media.docuemtns/document/image%A33

and FILE NAME: /document/image:33 are shown.**

I have tried both ACTION_OPEN_DOCUMENT and ACTION_GET_CONTENT actions with no success. Any suggestions or help will be greatly appreciated.

The URI and File name has to be correct.

Ryan M
  • 18,333
  • 31
  • 67
  • 74
  • The uri is correct. Its a nice content scheme. As you are not showing any code to determine the file name we cannot help you. – blackapps Apr 24 '23 at 17:56
  • `val file = File(data?.data?.path)` That is nonsense. You cannot use the File class for a content scheme uri. Using File class is not needed either. – blackapps Apr 24 '23 at 17:58
  • This poster knows how to obtain a file name for an uri. Hope the code is posted soon: https://stackoverflow.com/questions/76094229/android-13-photo-video-picker-file-name-from-the-uri-is-garbage – blackapps Apr 24 '23 at 18:00
  • The URI itself will have the file name at the end but I don’t see the file name at the end one is correct (image%A33). – Anitha Mannem Apr 24 '23 at 18:05
  • No. Uris do not have to have the display name at the end as you can see. The uri is correct and it does not contain a file name. I posted you a link. So you know already what you should do to obtain the file name. Wait for the code or google. – blackapps Apr 24 '23 at 18:07
  • Sure. When I try using ContentResolver for getting file name from URI it is returning Null. – Anitha Mannem Apr 24 '23 at 18:21
  • Show your code. (Not in a comment). – blackapps Apr 24 '23 at 18:22

0 Answers0