androidkotlinfirebase-storagegiphy-api

Error occured when using OnGifSelected to send a gif message


This is for my messaging app, whenever I try to send a gif from this GIPHY UI SDK, I get this error: An unknown error occurred, please check the HTTP result code and inner exception for server response.

  override fun onGifSelected(media: Media, searchTerm: String?, selectedContentType: GPHContentType) {


   val image = media.images.fixedWidth
        val  gif_url = image!!.gifUrl
        val gifUri = Uri.parse(gif_url)
        
        
        val reference2 = FirebaseDatabase.getInstance().getReference("Friends")
        reference2.child(firebaseUser!!.uid)
                .addListenerForSingleValueEvent(object : ValueEventListener {
                    override fun onDataChange(snapshot: DataSnapshot) {

                        if (snapshot.hasChild(visitorsUid!!)) {
                            //progressDialog
                            val pd = ProgressDialog(this@ChatActivity)
                            pd.setTitle("Please wait...")
                            pd.setMessage("Sending Gif...")
                            pd.setCanceledOnTouchOutside(false)
                            pd.show()

                            val databaseReference = FirebaseDatabase.getInstance().reference
                            //file name and path in firebase storage
                            val filenamePath = "Gif Images/" + firebaseUser!!.uid + System.currentTimeMillis()
                            val storageReference = FirebaseStorage.getInstance().getReference(filenamePath)
                            //upload image
                            storageReference.putFile(gifUri!!)
                                    .addOnSuccessListener { taskSnapshot ->
                                        //image uploaded, get url
                                        val p_uriTask = taskSnapshot.storage.downloadUrl
                                        while (!p_uriTask.isSuccessful);
                                        val p_downloadUri = p_uriTask.result
                                        if (p_uriTask.isSuccessful) {
                                            //image url recieved, save in db
                                            //timestamp
                                            val timestamp = "" + System.currentTimeMillis()

                                            //setup message data
                                            val hashMap = HashMap<String, Any>()
                                            hashMap["sender"] = firebaseUser!!.uid

I think the problem is with this line

 val gifUri = Uri.parse(gif_url)

I tried to upload it to my firebase storage

 storageReference.putFile(gifUri!!)

Here's what my logcat shows

 E/StorageException: No content provider: https://media3.giphy.com/media/iJbxNEqePoPp7l8I0g/200w.gif?cid=e8dbb930ge42y7jitdl6z424143u7ah6bti5nmz7v16mdi53&rid=200w.gif
    java.io.FileNotFoundException: No content provider: https://media3.giphy.com/media/iJbxNEqePoPp7l8I0g/200w.gif?cid=e8dbb930ge42y7jitdl6z424143u7ah6bti5nmz7v16mdi53&rid=200w.gif
        at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1969)
        at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1798)
        at android.content.ContentResolver.openInputStream(ContentResolver.java:1475)

Solution

  • There is no way to upload a file to Firebase Storage directly from a URL. You will first have to download the data from the URL, and then upload that local data to Firebase as either a String, a local File object, or a Stream.

    Also see: