"I am trying to upload a profile photo into firebase. but it's not retrieving into the application. when I upload a photo it's working. but not retrieving.how to fix this problem?."
'Here, when the user selects the image, here we can crop that image. and also here that image uploaded to the firebase.'
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == Gallery && resultCode == RESULT_OK && data!= null){
Uri imageUri = data.getData();
// start picker to get image for cropping and then use the image in cropping activity
CropImage.activity()
.setGuidelines(CropImageView.Guidelines.ON)
.setAspectRatio(1,1)
.start(this);
}
//crop image
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK){
progressDialog.setTitle("Set profile Image");
progressDialog.setMessage("Your profile image is updating..");
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.show();
Uri resultUri = result.getUri();
final StorageReference filePath = profileImageReference.child(currentUserID + ".jpg");//new profile image created
//upload image to firebase storage
filePath.putFile(resultUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
if (task.isSuccessful()){
Toast.makeText(SettingsActivity.this, "Profile Image uploaded...", Toast.LENGTH_SHORT).show();
final String downloadUri = filePath.getDownloadUrl().toString();
rootReference.child("Users").child(currentUserID).child("image")
.setValue(downloadUri)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()){
Toast.makeText(SettingsActivity.this, "Image Saved", Toast.LENGTH_SHORT).show();
}
else {
String message = task.getException().toString();
Toast.makeText(SettingsActivity.this, "Error: " + message, Toast.LENGTH_SHORT).show();
}
progressDialog.dismiss();
}
});
}
else {
String message = task.getException().toString();
Toast.makeText(SettingsActivity.this, "Error: " + message, Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
}
}
});
}
}
}
'In this part Retrieving user information and image file.'
private void RetrieveUserInformation() {
rootReference.child("Users").child(currentUserID)
.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if ((snapshot.exists()) && (snapshot.hasChild("name") && (snapshot.hasChild("image")))){
String retrieveUserName = snapshot.child("name").getValue().toString();
String retrieveDescription = snapshot.child("description").getValue().toString();
String retrieveProfileImage = (String) snapshot.child("image").getValue();
userName.setText(retrieveUserName);
description.setText(retrieveDescription);
Picasso.get().load(retrieveProfileImage).into(profileImage);
}
else if ((snapshot.exists()) && (snapshot.hasChild("name"))){
String retrieveUserName = snapshot.child("name").getValue().toString();
String retrieveDescription = snapshot.child("description").getValue().toString();
userName.setText(retrieveUserName);
description.setText(retrieveDescription);
}
else{
Toast.makeText(SettingsActivity.this, "Please, Update your information...", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}