当我尝试将图像上传到 Firebase 存储时,putFile 在 kotlin 中不起作用

Posted

技术标签:

【中文标题】当我尝试将图像上传到 Firebase 存储时,putFile 在 kotlin 中不起作用【英文标题】:putFile is not working in kotlin when I try to upload image to firebase storage 【发布时间】:2019-11-26 14:29:08 【问题描述】:

尝试将图像放置在 Firebase 存储中,但它似乎不起作用。

我已尝试更改规则并确保依赖项已打开并应用了所需的存储权限,但它似乎不起作用。

以下是用于对用户进行身份验证并上传其个人资料图片的代码,身份验证有效,但上传无效。

var selectedPhotoUri: Uri? = null

  override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) 
    super.onActivityResult(requestCode, resultCode, data)

    if (requestCode == 0 && resultCode == Activity.RESULT_OK && data != null && data.data != null) 
      // proceed and check what the selected image was....
      Log.d(TAG, "Photo was selected")

      selectedPhotoUri = data.data

      val bitmap = MediaStore.Images.Media.getBitmap(contentResolver, selectedPhotoUri)

      selectphoto_imageview_register.setImageBitmap(bitmap)

      selectphoto_button_register.alpha = 0f
    
  

 private fun performRegister() 
    val email = email_edittext_register.text.toString()
    val password = password_edittext_register.text.toString()

    if (email.isEmpty() || password.isEmpty()) 
      Toast.makeText(this, "Please enter text in email/pw", Toast.LENGTH_SHORT).show()
      return
    

    Log.d(TAG, "Attempting to create user with email: $email")

    // Firebase Authentication to create a user with email and password
    FirebaseAuth.getInstance().createUserWithEmailAndPassword(email, password)
        .addOnCompleteListener 
          if (!it.isSuccessful) return@addOnCompleteListener

          // else if successful
          Log.d(TAG, "Successfully created user with uid: $it.result!!.user.uid")

          uploadImageToFirebaseStorage()
        
        .addOnFailureListener
          Log.d(TAG, "Failed to create user: $it.message")
          Toast.makeText(this, "Failed to create user: $it.message", Toast.LENGTH_SHORT).show()
        
  

 private fun uploadImageToFirebaseStorage() 
    if (selectedPhotoUri != null)

      val progressDialog = ProgressDialog(this)
      progressDialog.setTitle("Uploading...")
      progressDialog.show()

      val filename = UUID.randomUUID().toString()
      val ref = FirebaseStorage.getInstance().getReference("/images/$filename")
      Log.d(TAG, "this is the data $selectedPhotoUri")



      ref.putFile(selectedPhotoUri!!)
              .addOnSuccessListener 
                Log.d(TAG, "Successfully uploaded image: $it.metadata?.path")
                progressDialog.hide()
                ref.downloadUrl.addOnSuccessListener 
                  Log.d(TAG, "File Location: $it")

                  saveUserToFirebaseDatabase(it.toString())
                
              

              .addOnFailureListener 
                Log.d(TAG, "Failed to upload image to storage: $it.message")
              

              .addOnProgressListener taskSnapshot ->
                val progress = 100.0 * taskSnapshot.bytesTransferred/taskSnapshot.totalByteCount
                progressDialog.setMessage("Uploaded " +progress.toInt())

              
    

当我按下注册按钮时,剩下的只是没有结束的进度对话框屏幕。 performRegister 函数在 onCreate 函数中执行。没有出现错误代码,我不确定是什么导致了问题。存储规则是公开的,所以我认为这也不是问题。

【问题讨论】:

【参考方案1】:

ref 是reference 类型的对象。在firebase 文档中,它没有称为putFile() 的方法。我认为是这样。如果要上传照片,可以使用put() 而不是putFile()selectedPhotoUri!! 参数应该是 blob 或文件类型。

ref.put(selectedPhotoUri!!) //selectedPhotoUri blob or file
          .addOnSuccessListener 
            Log.d(TAG, "Successfully uploaded image: $it.metadata?.path")
            progressDialog.hide()
            ref.downloadUrl.addOnSuccessListener 
              Log.d(TAG, "File Location: $it")

              saveUserToFirebaseDatabase(it.toString())
            
          

more details

【讨论】:

以上是关于当我尝试将图像上传到 Firebase 存储时,putFile 在 kotlin 中不起作用的主要内容,如果未能解决你的问题,请参考以下文章

当我将图像上传到 Firebase 时,来自 Database Realtime 的图像 url 与 firebase 存储中的图像 url 不同

尝试将图像上传到 Firebase 存储时出错

无法从 python 服务器将图像上传到 Firebase 存储

ios将图像上传到Firebase但是当我尝试下载时却不是这样吗?

将多个图像的异步图像上传到firebase存储

文件上传到 Firebase 存储不起作用(“存储/无效参数”)