简体   繁体   中英

Uploading a photo to Firebase Storage and updating the profile aatar

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;

Judging by the stack trace you posted here:

堆栈跟踪

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 .

I saw another question with the exact error code however the solution doesn't work for me in the question it was said to change the rules to
service firebase.storage {

match /b/{bucket}/o {

match /{userId} {

match /{allPaths=**}{


allow read, write: if request.auth.uid == userId;

}

}

}

i did the same but i still have the code error https://imgur.com/a/Di3uPdg

This is what worked for me

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM