使用当前用户的UID将图像上传到Firebase存储 [英] Upload Image to Firebase Storage with the current users UID

查看:111
本文介绍了使用当前用户的UID将图像上传到Firebase存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您如何上传同时附加了当前用户的UID的图像?

How do you upload an image that will also have the current user's UID attached?

以下示例.

这是我用来上传图片的代码,但到目前为止,添加UID都没有成功.

This is the code I am using to upload the image, but so far having no success adding on the UID.

private void updloadImage(Uri pickedImage){
        StorageReference myStorage = FirebaseStorage.getInstance().getReference().child("users_photos");
        final StorageReference imagePath = myStorage.child(pickedImage.getLastPathSegment());
        userID = fAuth.getCurrentUser().getUid();
        imagePath.putFile(pickedImage).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                imagePath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                    @Override
                    public void onSuccess(Uri uri) {
                        UserProfileChangeRequest myUpdate = new UserProfileChangeRequest.Builder().setPhotoUri(uri).build();


                    }
                });
            }
        });

推荐答案

如果您希望用户的UID成为Cloud Storage中映像文件名的一部分,则可以执行以下操作:

If you want the user's UID to be part of the image file name in Cloud Storage, you'd do:

StorageReference myStorage = FirebaseStorage.getInstance().getReference().child("users_photos");
final StorageReference imagePath = myStorage.child(pickedImage.getLastPathSegment());
userID = fAuth.getCurrentUser().getUid();
imagePath.putFile(userID+"_"+pickedImage).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {

如果您希望用户的图像基于用户的UID放在子文件夹中,则可以将最后一行更改为:

If you instead want the image(s) for a user to end up in a subfolder based on their UID, you'd change that last line to:

imagePath.child(userID).putFile(pickedImage).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {

这篇关于使用当前用户的UID将图像上传到Firebase存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆