Upload Image to Firebase Storage with the current users UID

回眸只為那壹抹淺笑 提交于 2021-02-15 07:43:36

问题


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

Example below.

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();


                    }
                });
            }
        });

回答1:


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>() {

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>() {


来源:https://stackoverflow.com/questions/60460257/upload-image-to-firebase-storage-with-the-current-users-uid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!