1

如果我给毕加索的 load() 方法提供任何照片 url 作为参数,照片会上传,但是当我给 load() 方法提供参数“image”时,如以下代码所示,图像不会显示在 android设备。设备显示占位符。

此外,onActivityResult() 方法中的 downloadUrl 字符串与图像字符串中的相同。

请帮忙!

    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();
        }
    }
}
4

0 回答 0