无法从意图onActivityResult获取文件uri [英] Can't get file uri from intent onActivityResult
问题描述
我想从 onActivityResult
接收正常的文件路径,如下所示:
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()
}
此文件也存在,但我无法获取.我在这里看到了一些问题,这些问题建议获取像这样的 REAL
路径:
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
所以,我在这里收到的 data !!.dataString !!
:
So, what I receive here data!!.dataString!!
:
content://com.android.providers.media.documents/document/image%3A18
以及我在这里收到的内容 Log.i("m",uri.path.toString())
:
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?)
我如何将文件转换为字符串:
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()
}
}
推荐答案
我想从onActivityResult接收正常的文件路径,如下所示:
I would like to receive normal file path from onActivityResult like this:
我的猜测是您使用的是 ACTION_OPEN_DOCUMENT
,或者也许是 ACTION_GET_CONTENT
.您将得到一个 content
Uri
,这正是这些 Intent
操作被记录返回的内容.这样的 Uri
可能有以下支持:
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:
- 外部存储上的本地文件,您可能无法在Android 10+上访问
- 内部存储中另一个应用程序的本地文件
- 可移动存储中的本地文件
- 已加密且需要动态解密的本地文件
- 数据库的
BLOB
列中保存的字节流 - 互联网上的内容首先需要由其他应用下载
- 动态生成的内容
- ...等等
此异常来自将我的文件转换为字符串的方法
this exception comes from method which converts my file to string
我假设通过将我的文件转换为字符串",是指以 String
的形式读取文件内容.在这种情况下:
I assume that by "converts my file to string", you mean read the file contents in as a String
. In that case:
- 通过在
- 获取
ContentResolver
> - 在
ContentResolver
上调用openInputStream()
,传入您的Uri
,在Uri
标识的内容 - 在
InputStream
上调用reader().readText()
以获取表示文件内容的String
Context
(例如您的 Activity
getContentResolver()来- 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() } }
这篇关于无法从意图onActivityResult获取文件uri的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!