无法从android中的图像uri获取图像真实路径



我试图从图像的uri中获取图像的真实路径,但作为路径返回null。我正在使用以下引用的代码来获得真实的路径。

https://gist.github.com/tatocaster/32aad15f6e0c50311626

示例图像Uri:content://com.android.providers.media.documents/document/image%3A102173

返回的真实路径:null

试试这个,它对我不起作用,但你可以尝试从mediastore中获取URI的文件名和路径,因为它显然对很多人都起作用。如果你仍然是空的,你可以做我做过的事。

我尝试了很多方法,但结果仍然是无效的。基本上,我试图从本地存储上传一个图像,对其进行压缩,并使用压缩后的图像。

val path = this.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS)`try {
val file = File(path, "/Image")
file.mkdirs()
val image = File(file.absolutePath, "image1.jpeg")
intent1.data = image.absolutePath.toUri()
val bitmap = BitmapFactory.decodeStream(this.contentResolver.openInputStream(uri))
val array = ByteArrayOutputStream()
val out =  FileOutputStream(image)
bitmap.compress(Bitmap.CompressFormat.JPEG, 70,out)
out.flush()
out.close()
Log.d("Path", image.absolutePath)
} catch (e: Exception) {
e.printStackTrace()
}`

最新更新