我想在不使用读写权限的情况下将图像上传到FTP 服务器。我已成功获取内容 uri,但无法找到图像的原始路径来获取输入流。
当我得到 uri content://com.android.providers.media.documents/document/image%3A1537 但我的原始路径不同,即内部存储/DCIM/Camera/IMG_20211026_11232.jpg
我正在使用新的 android 推荐ActivityResultLauncher
ActivityResultContracts.GetContent()
){
it?.let{
uriCallback.invoke(it)
this.uri = it
}
}
observer = ImageSetter(this, photoIv) { uri ->
val path = uri?.path
var openInputStream = contentResolver?.openInputStream(uri!!)
var format = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(Date())
var file = File(filesDir, "$format.jpg")
var fileoutputStream = FileOutputStream(file)
openInputStream?.copyTo(fileoutputStream)
openInputStream?.close()
fileoutputStream.close()
}
lifecycle.addObserver(observer)
observer.selectImage()
}