尝试将图像上传到Firebase存储, 已经添加的依赖项,存储是公共的,logcat中没有错误, 用户身份验证完美运行
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, "Fill fields", Toast.LENGTH_SHORT).show()
return
}
Log.d("RegisterActivity", "email is " + email)
Log.d("RegisterActivity", "password is $password")
FirebaseAuth.getInstance().createUserWithEmailAndPassword(email, password)
.addOnCompleteListener {
if (!it.isSuccessful) return@addOnCompleteListener
Log.d("RegisterActivity", "Succesfully created user with uid: ${it.result?.user?.uid}")
}
.addOnFailureListener {
Log.d("RegisterActivity", "Faild to create user ${it.message}")
Toast.makeText(this, "Faild to create user ${it.message}", Toast.LENGTH_SHORT).show()
}
}
private fun uploadImageToFirebaseStorage(){
if(SelectedPhotoUri==null)return
val filename=UUID.randomUUID().toString()
val ref=FirebaseStorage.getInstance().getReference("/images/$filename")
ref.putFile(SelectedPhotoUri!!)
.addOnSuccessListener {
Log.d("Register","succesfuly uploaded image: ${it.metadata?.path}")
}
}
logcat中没有错误
答案 0 :(得分:3)
要获取结果的图像URI开始活动。它将打开一个图像选择器:
val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.type = "image/jpeg"
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true)
startActivityForResult(
Intent.createChooser(intent, "Complete action using"),
RC_PHOTO_PICKER
)
在onActivityResult中检查是由图像选择器导致的。
if (requestCode == RC_PHOTO_PICKER && resultCode == Activity.RESULT_OK) {
pushPicture(data)
}
最后是将图像推送到Firebase存储的方法:
fun pushPicture(data: Intent?) {
val selectedImageUri = data!!.data
val imgageIdInStorage = selectedImageUri!!.lastPathSegment!! //here you can set whatever Id you need
storageReference.child(imgageIdInStorage).putFile(selectedImageUri)
.addOnSuccessListener { taskSnapshot ->
val urlTask = taskSnapshot.storage.downloadUrl
urlTask.addOnSuccessListener { uri ->
//do sth with uri
}
}
}