Firebase how to get Image Url from firebase storage?

后端 未结 12 578
悲&欢浪女
悲&欢浪女 2020-12-23 15:15

Right now i am fetching image from Storage of Firebase by using below code :

mStoreRef.child("photos/" + model.getBase64Image())
          .getDownl         


        
12条回答
  •  囚心锁ツ
    2020-12-23 15:33

    Yes that's possible !!. Instead of some creepy complicated lines of code here is my shortcut trick to get that downloadurl from firebase storage in kotlin

    Note:Based on the latest firebase release there is no getdownloadurl() or getresult() method (they are the prey of deprecition this time around)

    So the the trick i have used here is that by calling UploadSessionUri from the taskSnapshot object which in turn returns the downloadurl along with upload type,tokenid(one which is available for only shorter span of time) and with some other stuffs.

    Since we need only download url we can use substring to get downloadurl and concat it with alt=media in order to view the image.

    var du:String?=null
    var du1:String?=null
    var du3:String="&alt=media"
    val storage= FirebaseStorage.getInstance()
            var storagRef=storage.getReferenceFromUrl("gs://hola.appspot.com/")
            val df= SimpleDateFormat("ddMMyyHHmmss")
            val dataobj= Date()
            val imagepath=SplitString(myemail!!)+"_"+df.format(dataobj)+".jpg"
            val imageRef=storagRef.child("imagesPost/"+imagepath)
            val baos= ByteArrayOutputStream()
            bitmap.compress(Bitmap.CompressFormat.JPEG,100,baos)
            val data=baos.toByteArray()
            val uploadTask=imageRef.putBytes(data)
            uploadTask.addOnFailureListener{
                Toast.makeText(applicationContext,"Failed To Upload", Toast.LENGTH_LONG).show()
            }.addOnSuccessListener { taskSnapshot ->
                imageRef.downloadUrl.addOnCompleteListener (){
                    du=taskSnapshot.uploadSessionUri.toString()
                    du1=du!!.substring(0,du!!.indexOf("&uploadType"))
                    downloadurl=du1+du3
                    Toast.makeText(applicationContext,"url"+downloadurl, Toast.LENGTH_LONG).show()
                }
            }
    

    Hope it helps !.

提交回复
热议问题