3

I'm trying to open a file picker in android, select a json file then get the text from it. The app crashes after i select the file because it can not find the path.

I have tried adding external storage read / write permission and changing the path format

/// some Activity code
val myFileIntent=Intent()
.setType("*/*")
.setAction(Intent.ACTION_GET_CONTENT)
startActivityForResult(myFileIntent,10)

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        val path = data!!.data.path
        val jsonContent = File(path).readText()
    }
3
  • Please post the stacktrace Commented Apr 29, 2019 at 17:36
  • Hi, this might be of interest stackoverflow.com/a/36129285/235354 Commented Apr 29, 2019 at 17:37
  • Caused by: java.io.FileNotFoundException: /document/raw:/storage/emulated/0/Download/reteta.json (No such file or directory) at java.io.FileInputStream.open0(Native Method) at java.io.FileInputStream.open(FileInputStream.java:231) at java.io.FileInputStream.<init>(FileInputStream.java:165) at kotlin.io.FilesKt__FileReadWriteKt.readBytes(FileReadWrite.kt:63) at kotlin.io.FilesKt__FileReadWriteKt.readText(FileReadWrite.kt:101) Commented Apr 29, 2019 at 17:50

2 Answers 2

3

I'm trying to open a file picker in android

That is not a file picker. That allows the user to choose a piece of content, which may or may not be a file.

The app crashes after i select the file

You are not picking a file. You are picking a piece of content. That content is identified by a Uri, and the scheme of your Uri is content, not file.

Use ContentResolver and openInputStream() to get an InputStream on the content identified by the Uri. You can call readText() on that InputStream to read it in as text.

Sign up to request clarification or add additional context in comments.

3 Comments

Do you have any suggestion on how could i implement that ?
This can not be done . You can not call readText() on that InputStream to read it in as text.
@TeodorRadu: Ah, sorry, I was mis-remembering. You need to convert the InputStream into a BufferedReader: stackoverflow.com/a/39500046/115145
2

This is my final working code :

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        val path = data?.data
        val jsonSelectedFile =  contentResolver.openInputStream(path);
        val inputAsString = jsonSelectedFile.bufferedReader().use { it.readText() }

        Toast.makeText(this, "Json: " +  inputAsString , Toast.LENGTH_LONG).show()
    }

And for calling :

 importJsonButton.setOnClickListener {
            val myFileIntent = Intent()
                .setType("*/*")
                .setAction(Intent.ACTION_GET_CONTENT)

            startActivityForResult(myFileIntent,10)
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.