Home > Blockchain >  How to upload an image to storage firebase from an url
How to upload an image to storage firebase from an url

Time:06-14

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

CodePudding user response:

My Method of setting a selected Image to a image view to then be Uploaded is

private val ImageLauncher = registerForActivityResult(ActivityResultContracts.GetContent()){ uri: Uri? ->
       uri?.let { binding.GroupImageAdd.setImageURI(uri)
       ImageUri = uri
       }
    }

The Uri is a Local val set when the images is selected from file and then

 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()}")
        }

    }

Unsure if this helps

CodePudding user response:

The Firebase Storage SDK doesn't allow you to directly upload the contents of random http URLs like the one you're showing here. The putFile method requires a Uri from a ContentProvider that's serviced by an Android app on the device. That's not what you're doing here at all.

If you want to upload an image that exists outside your app, you will have to download it first locally, then upload the data that you downloaded using putBytes, putStream or putFile.

  • Related