0

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

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
luke cross
  • 321
  • 1
  • 2
  • 19

1 Answers1

1

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

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
  • But if I do that the savings in the database do not exist, correct? In fact I would just add extra space – luke cross Apr 28 '20 at 13:20
  • It will create the resized image and keep the original one. – Renaud Tarnec Apr 28 '20 at 13:21
  • Keeping the original consumption at the bank is equal – luke cross Apr 28 '20 at 13:25
  • I'm not sure to understand what you exactly mean by "Keeping the original consumption at the bank is equal" – Renaud Tarnec Apr 28 '20 at 13:27
  • What I want is for the uploaded image to be smaller so that it doesn't consume so much in the database, as there are users who upload 10mb images, – luke cross Apr 28 '20 at 13:28
  • If I keep the image bigger and smaller, I would actually have (for example) 10mb + about 80kb of the resized image, the right thing would be to reduce the consumption of 10mb to simply 80kb. – luke cross Apr 28 '20 at 13:29
  • 1
    Have a look at the update to my answer: you either need to implement the resizing process yourself or you need to update the image url to the new one which was resized by the Firebase extension. Firebase extensions are great because they allow you to rapidly deploy a standard business logic but they might be limited in terms of configuration options. – Renaud Tarnec Apr 28 '20 at 13:31
  • I'm not sure if it does anything very different from the current one, but thanks, I'll try! – luke cross Apr 28 '20 at 13:41
  • 1
    Indeed it is not very different but you have **full control** on the process. So you can delete the original image and rename the new one with the same name. – Renaud Tarnec Apr 28 '20 at 13:46