0

I need the user to pick a file to post on the server and for that inside my activity I am opening a file explorer activity like this:

val intent = Intent()
            .setType("*/*")
           .setAction(Intent.ACTION_GET_CONTENT)
        startActivityForResult(Intent.createChooser(intent, "Select a file"), 111)

For that I am overriding onActivityResult like this:

 override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 111 && resultCode == RESULT_OK) {
            var currentUriContent:Uri?=null
            data?.let {
                currentUriContent=it.data //The uri with the location of the file
            }


        val selectedFile = File(currentUriContent?.path) 
        Log.d("File exists",selectedFile.exists().toString())
        Log.d("file name",selectedFile.name)
        Log.d("selected file uri", currentUriContent.toString())

And I am getting this console output..

 D/File exists: false
D/file name: msf:32
D/selected file uri: content://com.android.providers.downloads.documents/document/msf%3A32

How can I open a selected file from the content URI? I have searched but can't find similar solutions in Kotlin. I have read/write access, but I can't get the proper file name in the log, or file URI for opening the file. What am I doing wrong?

EDIT: For those that may run into the same problem, here was the solution (that might save you a couple of clicks.) So as stated below, the approach was wrong. I had to use and implement a file picking library, with which I got the path that you normally wanted to get, for example storage/emulated..etc Here is the link to what I used https://android-arsenal.com/details/1/8131#!description

The code I used was called in the main activity and looks something like this

if (permissionGranted()) {
            val singleFilePickerDialog = SingleFilePickerDialog(this,
                {
                    Toast.makeText(
                        this,
                        "Canceled!!",
                        Toast.LENGTH_SHORT
                    ).show()
                }
            ) { files: Array<File> ->
                Toast.makeText(
                    this,
                    files[0].path,
                    Toast.LENGTH_SHORT
                ).show()
                //saving the URI and using it for my file
                myFileURI = files[0].path
                Log.d("myfile uri test",myFileURI)
                val myFile = File(myFileURI)
                Log.d("myfile exists",myFile.exists().toString())
                Log.d("myfile name",myFile.name)
            }
            singleFilePickerDialog.show()
        } else {
            requestPermission()
        }

Thank you for your help.

Pers
  • 15
  • 8
  • 1
    InputStream is = getContentResolver().openInputStream(data.getData()); Use as if it was a FileInputStream. You do not need read/write access stuff. – blackapps Mar 20 '21 at 11:32
  • "I am opening a file explorer activity" -- that is not a file explorer. – CommonsWare Mar 20 '21 at 12:23

0 Answers0