luke cross
luke cross

Reputation: 331

Update profile photo after images resized with Firebase extension

during the creation of my user I insert your photos in the Firebase Storage database (as shown in the method below):

 final StorageReference ref = FirebaseStorage.getInstance().getReference("/images/"+barNomeUser.getText().toString()+"perfilFoto"); 
    ref.putFile(uriSelecionada) // Insere a foto selecionada no Storage
            .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() { 
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    ref.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                        @Override
                        public void onSuccess(Uri uri) { 
                            Log.i("Url do firebase", uri.toString());
                            uriSelecionada = uri; 
                            String uuid = FirebaseAuth.getInstance().getUid(); 
                            final String nameUser = barNomeUser.getText().toString(); //
                            String profileFotoUrl = uriSelecionada.toString(); 
                            final UserApp userApp = new UserApp(uuid, nameUser, profileFotoUrl, 0, 0, 0, 0);
                            UserProfileChangeRequest profileChangeRequest = new UserProfileChangeRequest.Builder()
                                    .setDisplayName(nameUser).setPhotoUri(uri).build();
                            firebaseUser.updateProfile(profileChangeRequest).addOnCompleteListener(new OnCompleteListener<Void>() {
                                @Override
                                public void onComplete(@NonNull Task<Void> task) {
                                    if(task.isSuccessful()){
                                        Log.i("Register full", firebaseUser.getDisplayName());
                                    }
                                } ...

However, I noticed that it consumed a lot of database memory, and I realized that the cloudFunctions storage-resize function would solve my problem ... and it even solved it, but, it generated another bigger problem, the original image is deleted after being resized , this makes the previous access token (taken by the getDownloadUrl method) no longer valid, as that image was deleted from Storage and now what exists is a new image, and a new token, leaving the user without a "photo" in profile, how could I be able to pull the resized image to update my user's profile photo if the resizing process can take up to 60 seconds and, in addition, there is a time to query the database?

The new image looks like it gets an equal token, with the only difference of having a "_480x320" added at the end: imageExample

Upvotes: 0

Views: 525

Answers (1)

Renaud Tarnec
Renaud Tarnec

Reputation: 83181

You have the possibility, when configuring the "Resize Images" Firebase extension to choose if you want to delete, or not, the original file.

You should select "No" as shown below.

enter image description here


If you want to have more control on the process (e.g. delete the original image and assign its name to the new (resized) one), you'll have to implement the image resizing process yourself. For that you can modify this official Cloud Function sample: https://github.com/firebase/functions-samples/tree/master/generate-thumbnail

Upvotes: 1

Related Questions