当我从edamam API获得ImageURI并尝试上传它时,它会给出一些错误,当我在拍照时转换位图并将其转换为URI时,我没有得到,我试图将ImageURI转换为位图以将其传递给相同的函数,但这不起作用。下面这个函数是有效的
fun bitmapToUri(imageBitmap: Bitmap, cacheDir: File): Uri {
val baos = ByteArrayOutputStream()
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos)
data2 = baos.toByteArray()
val file = File(cacheDir, imageBitmap.toString())
// When calling it with imageBitmap.toString() each item has its image (before the same one repeated)
file.delete()
// Just in case there is another File
file.createNewFile()
val fileOS = FileOutputStream(file)
fileOS.write(data2)
fileOS.flush()
fileOS.close()
baos.close()
return file.toUri()
}
我得到这些错误
E/UploadTask: could not locate file for upload:https://www.edamam.com/food-img/28a/28ae0e32feff919253b2cd17c47d2f23.jpgE/StorageException: StorageException has occurred.处理步骤发生未知错误,请检查HTTP结果代码和服务器响应的内部异常。-13000 HttpResult: 0E/StorageException: No content provider: https://www.edamam.com/food-img/28a/28ae0e32feff919253b2cd17c47d2f23.jpgjava.io.FileNotFoundException: No content provider: https://www.edamam.com/food-img/28a/28ae0e32feff919253b2cd17c47d2f23.jpg
这是我要上传到Storage的函数
private fun uploadImage() {
val name = binding.inputName?.editText?.text.toString()
val provider = binding.inputProvider?.editText?.text.toString()
val address = binding.inputStreet?.editText?.text.toString()
val fileName = "$name $provider $address"
Log.d("INFO IMAGE URI UPLOAD->>", imageUri.toString())
storage = FirebaseStorage.getInstance().getReference("images/$fileName")
storage.putFile(imageUri).addOnSuccessListener {
binding.imageButton.setImageURI(null)
}.addOnFailureListener {
Toast.makeText(this@NewItem, "Image Not Uploaded", Toast.LENGTH_SHORT).show()
}
}
这是我传递给imageUri的值
imageUri = apiCallBody.listHints.first().food.image.toUri()
这个值是什么
https://www.edamam.com/food-img/862/862434152a3191f30c889f10eb2989b0.jpgFirebase Storage SDK不允许您直接上传随机http url的内容,就像您在这里显示的那样。putFile方法需要一个来自ContentProvider的Uri,这个ContentProvider由设备上的Android应用提供服务。这根本不是你来这儿的目的。
如果你想上传一个存在于应用程序之外的图像,你必须首先在本地下载它,然后上传你使用putBytes, putStream或putFile下载的数据。
我将所选图像设置为待上传的图像视图的方法是
private val ImageLauncher = registerForActivityResult(ActivityResultContracts.GetContent()){ uri: Uri? ->
uri?.let { binding.GroupImageAdd.setImageURI(uri)
ImageUri = uri
}
}
Uri是一个Local值设置何时从文件中选择图像然后
fun UploadImage(UUid: String, uri: Uri, ImageType: String) {
val storageRef = FirebaseStorage.getInstance().reference
Log.d("URI", uri.toString())
val task = storageRef.child("**Image Directory").putFile(uri)
task.addOnSuccessListener {
Log.d("UploadImage", "Task Is Successful")
}.addOnFailureListener {
Log.d("UploadImageFail", "Image Upload Failed ${it.printStackTrace()}")
}
}
不确定这是否有帮助