Uploading a photo to Firebase Storage and updating the profile aatar

61 views Asked by At

Hello So basically when clicking on the image view the dialog opens and i can choose a picture however when the picture is selected the app get's me disconnected from the account without uploading and updating the picture .

 dialog = new SpotsDialog.Builder(getContext()).create();
    storageReference=FirebaseStorage.getInstance().getReference("image_upload");

    avatarIv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent =new Intent();
            intent.setType("image/");
            intent.setAction(intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent,"Choisissez une image"),PICK_IMAGE_CODE);
        }
    });



 @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == PICK_IMAGE_CODE ){
        dialog.show();
        UploadTask uploadTask =storageReference.putFile(data.getData());
        Task<Uri>task= uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
            @Override
            public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
                if (!task.isSuccessful()){
                    Toast.makeText(getActivity(),"Erreur de téléchargement",Toast.LENGTH_SHORT).show();

                }
                return  storageReference.getDownloadUrl();
            }
        }).addOnCompleteListener(new OnCompleteListener<Uri>() {
            @Override
            public void onComplete(@NonNull Task<Uri> task) {
                if(task.isSuccessful()){
                    String Url =task.getResult().toString().substring(0,task.getResult().toString().indexOf("&token"));
                    Log.d("DIRECTLINK",Url);
                    Picasso.get().load(Url).into(avatarIv);
                    dialog.dismiss();
                }
            }
        });
    }
}

By using a breakpoint in if(requestCode == PICK_IMAGE_CODE ) i got this

Debug LOG

Ps: this code is implemented in a fragment (Bottom navigation View)

private static final int PICK_IMAGE_CODE = 1000;

Edit : these are the Storage rules allow read, write: if request.auth != null;

2

There are 2 answers

2
ᴇʟᴇvᴀтᴇ On

Judging by the stack trace you posted here:

Stack trace

It seems that you have a classpath / classloader issue. Make sure you are using the same version for all the Firebase libraries. Check your classpath.

See this similar question.

1
Omar Skouri On

This is what worked for me

match /b/{bucket}/o {
  match /{userId} {
    match /{allPaths=**}{
     allow read 
     allow write;
    }
  }
}