0

I would like to receive normal file path from onActivityResult like this:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK) {
            Log.i("m", data!!.dataString!!)
            convertFileToString(data.dataString!!)

        }
    }

but I receive such error:

java.io.FileNotFoundException: File 'content:/com.android.providers.media.documents/document/image%3A18' does not exist

this exception comes from method which converts my file to string. This error points to this line:

try {
val data = FileUtils.readFileToByteArray(file) // this line

} catch (e: IOException) {
e.printStackTrace()
}

This file exists also but I can't get it. I saw here some questions which suggest to get REAL path like this:

fun getPath(context:Context, uri:Uri):String {
  val result:String = null
  val proj = arrayOf<String>(MediaStore.Images.Media.DATA)
  val cursor = context.getContentResolver().query(uri, proj, null, null, null)
  if (cursor != null)
  {
    if (cursor.moveToFirst())
    {
      val column_index = cursor.getColumnIndexOrThrow(proj[0])
      result = cursor.getString(column_index)
    }
    cursor.close()
  }
  if (result == null)
  {
    result = "Not found"
  }
  return result
}

but this method return such exception:

java.io.FileNotFoundException: File 'Not found' does not exist

So, what I receive here data!!.dataString!!:

content://com.android.providers.media.documents/document/image%3A18

and what I receive here Log.i("m",uri.path.toString()):

/document/image:18

as I see this is not real path on which this pict was saved. Maybe someone knows where I made errors?)

UPDATE

How I convert file to string:

fun convertFileToString(path: String) {
        //dialog.dismiss()
        val file = File(path)

        for (i in 0 until sn.array!!.size()) {
            val jsonObj = sn.array!![i].asJsonObject
            val nFile = jsonObj.get("filename").asString

            if (file.name == nFile) {
                Toast.makeText(this, R.string.message_about_attached__file, Toast.LENGTH_SHORT).show()
                return
            }
        }

        try {
            val data = FileUtils.readFileToByteArray(file)
            uploadFiles(File(path).name, Base64.encodeToString(data, Base64.NO_WRAP))
        } catch (e: IOException) {
            e.printStackTrace()
        }
    }
Andrew
  • 1,947
  • 2
  • 23
  • 61
  • uri.data.toSting() will give you a nice content scheme. Use that. Dont try to get a file path. You dont need it. – blackapps Jan 25 '20 at 17:27

1 Answers1

2

I would like to receive normal file path from onActivityResult like this:

My guess is that you are using ACTION_OPEN_DOCUMENT, or perhaps ACTION_GET_CONTENT. You are getting a content Uri, which is exactly what those Intent actions are documented to return. Such a Uri might be backed by:

  • A local file on external storage, which you probably cannot access on Android 10+
  • A local file on internal storage for the other app
  • A local file on removable storage
  • A local file that is encrypted and needs to be decrypted on the fly
  • A stream of bytes held in a BLOB column in a database
  • A piece of content on the Internet that needs to be downloaded by the other app first
  • Content that is generated on the fly
  • ...and so on

this exception comes from method which converts my file to string

I assume that by "converts my file to string", you mean read the file contents in as a String. In that case:

  • Get a ContentResolver by calling getContentResolver() on a Context, such as your Activity
  • Call openInputStream() on the ContentResolver, passing in your Uri, to get an InputStream on the content identified by the Uri
  • Call reader().readText() on the InputStream to get a String representing the file contents

Combined, that should be something like:

val string = data.data?.let { contentResolver.openInputStream(it).use { it.reader().readText() } }
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • what does contain variable `string`? content of file or what? – Andrew Jan 25 '20 at 17:31
  • I added to my question method which converts my files to string, and here I have to send to it path in string – Andrew Jan 25 '20 at 17:34
  • @Andrew: It will contain the text in the content identified by the `Uri`, or `null` if you did not get a `Uri` for some reason. You keep referring to "file" -- the sooner you stop thinking in terms of files, the more success you will have. – CommonsWare Jan 25 '20 at 17:34
  • but then I need to convert this file to Base64, how I can use your solution for this task ?( – Andrew Jan 25 '20 at 17:36
  • @Andrew: There are plenty of existing answers for how to convert a string to Base64, including ones that you can find using any major search engine. [Here is one](https://stackoverflow.com/a/7687912/115145). However, you may be better served then using `readBytes()` rather than `reader().readText()`, to get a `ByteArray` that you can then convert to Base64. – CommonsWare Jan 25 '20 at 17:39