问题
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 callinggetContentResolver()
on aContext
, such as yourActivity
- Call
openInputStream()
on theContentResolver
, passing in yourUri
, to get anInputStream
on the content identified by theUri
- Call
reader().readText()
on theInputStream
to get aString
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