无法访问Firebase上传的文件网址

时间:2019-08-05 13:36:54

标签: android firebase kotlin firebase-storage

我已经使用以下代码:

RigidBodyPlant

还要使用此代码:

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

    super.onActivityResult(requestCode, resultCode, data)
    if (resultCode == Activity.RESULT_OK && requestCode == PICTURE_RESULT) {
        if (data != null) {

            val imageUri: Uri = data.data!!
            val ref: StorageReference = FirebaseUtil.storageRef!!.child(imageUri.lastPathSegment!!)

            // Another implementation
            // UploadTask uploadTask = ref.putFile(imageUrl)
            ref.putFile(imageUri).addOnSuccessListener(this) {
                ref.downloadUrl.addOnCompleteListener(this) { task ->
                    val url = task.result.toString()
                    deal!!.imageUrl = url
                }
            }


        } else {
            Log.d("IMAGE", resultCode.toString())
            Toast.makeText(this, "Upload failure", Toast.LENGTH_LONG).show()
        }

    }

    finish()
}

我可以从我的Android调试窗口中看到URI,但是未在firebase项中设置该URI,因为我正在使用最新的firebase最新数据库库。我可以在Android Studio的调试窗口中看到此值。 但是该值不用于设置我的Firebase条目的图像。我正在使用的库:

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

    super.onActivityResult(requestCode, resultCode, data)
    if (resultCode == Activity.RESULT_OK && requestCode == PICTURE_RESULT ) 
    {
        if (data != null) {

            val imageUri : Uri = data.data!!
            val ref : StorageReference = FirebaseUtil.storageRef!!.child(imageUri.lastPathSegment!!)

            // Another implementation
            // UploadTask uploadTask = ref.putFile(imageUrl)
            ref.putFile(imageUri).addOnSuccessListener(this, object : OnSuccessListener<UploadTask.TaskSnapshot> {
                override fun onSuccess(taskSnapshot : UploadTask.TaskSnapshot?) {
                    val url : String = taskSnapshot!!.storage.downloadUrl.toString()
                    deal!!.imageUrl = url
                }
            })

        } else {
            Log.d("IMAGE", resultCode.toString())
            Toast.makeText(this, "Upload failure", Toast.LENGTH_LONG).show()
        }

    }
}

0 个答案:

没有答案