Is there a way in which I messed up the code for downloading my profile pic from Firebase Realtime Database?

32 Views Asked by At

If i give to the Picasso's load() method any photo url as parameter, the photo uploads, but when i give the load() method the parameter "image" as you can see in following code, the image is not displayed on the android device. The device displays the placeholder.

Also, the downloadUrl string form the onActivityResult() method is the same as the one from the image string.

Please help!

    currentUserId = mAuth.getCurrentUser().getUid();
    usersRef = FirebaseDatabase.getInstance().getReference().child("Users").child(currentUserId);

profileImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent galleryIntent = new Intent();
            galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
            galleryIntent.setType("image/*");
            startActivityForResult(galleryIntent, galleryPick);
        }
    });

    usersRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if(dataSnapshot.exists()){
                if(dataSnapshot.hasChild("profileimage")){
                    String image = dataSnapshot.child("profileimage").getValue().toString();

                    //Glide.with(SetupActivity.this).load(image).placeholder(R.drawable.profile).into(profileImage);
                    Picasso.get().load(image).placeholder(R.drawable.profile).into(profileImage);
                } else {
                    Toast.makeText(SetupActivity.this, "Please select profile image first...", Toast.LENGTH_SHORT).show();
                }
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == galleryPick && resultCode == RESULT_OK && data!=null){
        Uri imageUri = data.getData();

        CropImage.activity(imageUri)
                .setGuidelines(CropImageView.Guidelines.ON)
                .setAspectRatio(1, 1)
                .start(this);
    }
    if(requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE){
        CropImage.ActivityResult result = CropImage.getActivityResult(data);

        if(resultCode == RESULT_OK){
            loadingBar.setTitle("Profile image");
            loadingBar.setMessage("Please wait, while we're updating your profile image...");
            loadingBar.show();
            loadingBar.setCanceledOnTouchOutside(true);

            Uri resultUri = result.getUri();

            StorageReference filePath = userProfileImageRef.child(currentUserId + ".jpg");

            filePath.putFile(resultUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
                    if(task.isSuccessful()){
                        Toast.makeText(SetupActivity.this, "Profile image successfully stored in Firebase database...", Toast.LENGTH_SHORT).show();
                        final String downloadUrl = task.getResult().getStorage().getDownloadUrl().toString();

                        usersRef.child("profileimage").setValue(downloadUrl)
                                .addOnCompleteListener(new OnCompleteListener<Void>() {
                                    @Override
                                    public void onComplete(@NonNull Task<Void> task) {
                                        if(task.isSuccessful()){
                                            Intent selfIntent = new Intent(SetupActivity.this, SetupActivity.class);
                                            startActivity(selfIntent);

                                            Toast.makeText(SetupActivity.this, "Profile image stored to Firebase database successfully", Toast.LENGTH_SHORT).show();
                                            loadingBar.dismiss();
                                        } else{
                                            String message = task.getException().getMessage();
                                            Toast.makeText(SetupActivity.this, "Error occured: " + message, Toast.LENGTH_SHORT).show();
                                            loadingBar.dismiss();
                                        }
                                    }
                                });
                    }
                }
            });
        } else {
            Toast.makeText(this, "Error occured: Image can't be cropped. Try again!", Toast.LENGTH_SHORT).show();
            loadingBar.dismiss();
        }
    }
}
0

There are 0 best solutions below