Can't get file uri from intent onActivityResult

心已入冬 提交于 2021-01-29 22:40:45

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/59911568/cant-get-file-uri-from-intent-onactivityresult

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!