When I get the ImageURI from the edamam API and try to upload it it gives some errors, which I am not getting when I transform the BitMap I get when taking a picture and transform it into URI, I have tried to transform the ImageURI into Bitmap to pass it to the same function but that doesnt work. This below is the function that does work
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()
}
I am getting these errors
E/UploadTask: could not locate file for uploading: https://www.edamam.com/food-img/28a/28ae0e32feff919253b2cd17c47d2f23.jpg E/StorageException: StorageException has occurred. An unknown error occurred, please check the HTTP result code and inner exception for server response. Code: -13000 HttpResult: 0 E/StorageException: No content provider: https://www.edamam.com/food-img/28a/28ae0e32feff919253b2cd17c47d2f23.jpg java.io.FileNotFoundException: No content provider: https://www.edamam.com/food-img/28a/28ae0e32feff919253b2cd17c47d2f23.jpg
This is the function I have to upload to 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()
}
}
And this is the value I'm passing to imageUri
imageUri = apiCallBody.listHints.first().food.image.toUri()
Which is this value
https://www.edamam.com/food-img/862/862434152a3191f30c889f10eb2989b0.jpg