[我正在尝试构建类似Instagram的应用程序,当我尝试将拍摄的照片保存到Firebase数据库上用户的文件夹“ images”时出现问题。它会继续保存类似“ com.google.firebase.storage.UploadTask@62873ce”的内容]
这是我的共享活动.kt
class ShareActivity : BaseActivity(2) {
private val TAG = "ShareActivity"
private lateinit var mCamera: CameraHelper
private lateinit var mFirebase: FirebaseHelper
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_share)
Log.d(TAG, "onCreate")
mFirebase =FirebaseHelper(this)
mCamera = CameraHelper(this)
mCamera.takeCameraPicture()
back_image.setOnClickListener{finish()}
share_text.setOnClickListener{share()}
}
@SuppressLint("MissingSuperCall")
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == mCamera.REQUEST_CODE){
if (resultCode == RESULT_OK){
GlideApp.with(this).load(mCamera.imageUri).centerCrop().into(post_image)
} else{
finish()
}
}
}
private fun share(){
val imageUri = mCamera.imageUri
if (imageUri != null){
val uid = mFirebase.auth.currentUser!!.uid
mFirebase.storage.child("users").child(uid).child("images")
.child(imageUri.lastPathSegment!!).putFile(imageUri).addOnCompleteListener {
if (it.isSuccessful){
mFirebase.database.child("images").child(uid).push()
.setValue(it.toString())
.addOnCompleteListener{
if (it.isSuccessful){
startActivity(Intent(this,
ProfileActivity::class.java))
finish()
} else {
showToast(it.exception!!.message!!)
}
}
} else {
showToast(it.exception!!.message!!)
}
}
}
}
}
这是FirebaseHelper.kt中的我的uploadUserPhoto和updateUsePhoto函数
fun uploadUserPhoto(photo: Uri, onSuccess: (String) -> Unit) {
val uTask = storage.child("users/${auth.currentUser!!.uid}/photo").putFile(photo)
storage.child("users/${auth.currentUser!!.uid}/photo").putFile(photo)
.addOnCompleteListener {
if (it.isSuccessful) {
uTask.continueWithTask { _ ->
storage.child("users/${auth.currentUser!!.uid}/photo").downloadUrl
}.addOnCompleteListener{
if (it.isSuccessful && it.result != null) {
onSuccess(it.result.toString())
} else {
activity.showToast(it.exception!!.message!!)
}
}
}
}
}
fun updateUserPhoto( photoUrl: String, onSuccess: () -> Unit){
database.child("users/${auth.currentUser!!.uid}/photo").setValue(photoUrl)
.addOnCompleteListener {
if (it.isSuccessful) {
onSuccess()
} else {
activity.showToast(it.exception!!.message!!)
}
}
}
我不确定如何设置我的private fun share()
将正确的URL上传到用户的“图像”文件夹中
这是我的CameraHelper.kt
class CameraHelper(private val activity: Activity){
var imageUri: Uri? = null
val REQUEST_CODE = 1
private val simpleDateFormat = SimpleDateFormat(
"yyyyMMdd_HHmmss",
Locale.US
)
fun takeCameraPicture() {
val intent =
Intent(MediaStore.ACTION_IMAGE_CAPTURE)
if (intent.resolveActivity(activity.packageManager) != null) {
val imageFile = createImageFile()
imageUri = FileProvider.getUriForFile(
activity,
"com.example.homeactivity.fileprovider",
imageFile
)
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri)
activity.startActivityForResult(intent, REQUEST_CODE)
}
}
private fun createImageFile(): File {
val storageDir: File? = activity.getExternalFilesDir(
Environment.DIRECTORY_PICTURES
)
return File.createTempFile(
"JPEG_${simpleDateFormat.format(Date())}_",
".jpg",
storageDir
)
}
}
it.toString()
不是URL。 it
是putFile()
返回的UploadTask对象。您不能只是将其转换为字符串-这是您需要观察以获取结果的Task。您似乎正在使用uploadUserPhoto
来获取要写入的URL,从而在downloadUrl
中正确获取了下载URL。您将必须采用这种方式。
另请参见documentation和this question。