0
votes

I would like to save a profile picture of a user in my database and to use it later.

I saved it to my firebase cloud storage with this code :

    private void uploadImage() {

    if(picture != null)
    {
        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setTitle("Uploading...");
        progressDialog.show();

        StorageReference ref = storageReference.child("images"+ UUID.randomUUID().toString());
        ref.putFile(picture)
                .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                        progressDialog.dismiss();

                        Toast.makeText(MainActivity.this, "Uploaded", Toast.LENGTH_SHORT).show();
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        progressDialog.dismiss();
                        Toast.makeText(MainActivity.this, "Failed "+e.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                })
                .addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                        double progress = (100.0*taskSnapshot.getBytesTransferred()/taskSnapshot
                                .getTotalByteCount());
                        progressDialog.setMessage("Uploaded "+(int)progress+"%");
                    }
                });
    }
}

Now, I want to save it as a child in my Database... I have this code to save all the datas (Name, email, phone, password...)

mAuth.createUserWithEmailAndPassword(edtEmail.getText().toString(), edtPassword.getText().toString())
                    .addOnSuccessListener(new OnSuccessListener<AuthResult>() {
                        @Override
                        public void onSuccess(AuthResult authResult) {
                            User user = new User();
                            user.setEmail(edtEmail.getText().toString());
                            user.setPhone(edtPhone.getText().toString());
                            user.setPassword(edtPassword.getText().toString());
                            user.setName(edtName.getText().toString());
                            user.setId(mAuth.getCurrentUser().getUid());
                            user.setProfilePic(picture);

But the "user.setProfilePic(picture)" doesn't work...

What could be the best way to save the profile picture of the user on my database ?

Thank you

2

2 Answers

1
votes

Thats not the right way of doing it. What you should do is,

1) Store images in storage it self, with some UUID.

2) Store that reference of image in the storage.

3) Get the data from database use libraries like Glide or Picasso to load the images to avoid OutOfMemory Exception.

Here is the example

 StorageReference ref = storageReference.child("images"+ UUID.randomUUID().toString());
    ref.putFile(picture)
            .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    progressDialog.dismiss();
               Uri downloadUri = taskSnapshot.getMetadata().getDownloadUrl();
               String downloadurl = mDownloadUrl.toString();

                }
            })

           );

And now

  User user = new User();
  user.setEmail(edtEmail.getText().toString());
  user.setPhone(edtPhone.getText().toString());
  user.setPassword(edtPassword.getText().toString());
  user.setName(edtName.getText().toString());
  user.setId(mAuth.getCurrentUser().getUid());
  user.setProfilePic(downloadurl);  //here picture URL

And final Step just to push the data to DataBase

String key = mDatabase.child("YourNodeHere").push().getKey();
mDatabase.child("YourNodeHere").child(key).setValue(user);
0
votes

To save the profile picture in the database try this:

         ref=db.child(mCurrentUser.getUid());
         StorageReference filepath=mStorage.child(uri.getLastPathSegment()); 
          filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() { 
             @Override
           public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { 
           String downloadurl = taskSnapshot.getDownloadUrl().toString(); 
           ref.child("image").setValue(downloadurl);
           //can save other data also

            }
      });

First add the url of the image in the firebase storage, then if added successfully, you will be able to retrieve the url String downloadurl = taskSnapshot.getDownloadUrl().toString(); and add it to the database child("image").setValue(downloadurl);

more info here:

https://firebase.google.com/docs/storage/android/download-files