首页 > 解决方案 > 从云存储中检索用户图像到 Imageview

问题描述

我正在使用 java 开发一个 android 应用程序,我正在使用 cloud firestore 和 firebase storage 来存储用户的个人资料图片。当用户登录时,他/她可以上传个人资料图片并将其加载到imageView中,图像成功上传到存储并成功加载到imageView中。

但是,当我重新运行应用程序并登录我上传图片的帐户时,无法像以前那样将图片加载到 imageView 中。

这是我上传图片的代码:

 if (mImageUri != null){
           file = mStorageRef.child(userId+".png");
            UploadTask = file.putFile(mImageUri).addOnSuccessListener(newOnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
 Toast.makeText(EditDonatorProfile.this,"Image uploaded successfully",Toast.LENGTH_SHORT).show();
                  String Link = mImageUri.toString(); 
                    Upload upload = new Upload(userId,Link);
                    db.collection("profilePicture").document(userId).set(upload);
                }
            });
        }
        else
        {
            Toast.makeText(this,"Choose a file first",Toast.LENGTH_SHORT).show();
        }

这是我的检索代码:

DocumentReference documentReference1 =db.collection("profilePicture").document(userId);
        documentReference1.addSnapshotListener(this, new EventListener<DocumentSnapshot>() {
            @Override
public void onEvent(@Nullable DocumentSnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) {
               String uri = documentSnapshot.getString("link");
          StorageReference storageReference = StorageReference.getReference("Images/"+userId+".png");
                if(uri == null)
                  return;
                       Uri link = Uri.parse(String.valueOf(uri));
                       Picasso.with(DonatorProfile.this).load(link).into(Photo);
            }
        });

标签: javaandroidfirebasegoogle-cloud-firestoregoogle-cloud-storage

解决方案


getCurrentUser().updateProfile(new UserProfileChangeRequest.Builder().setPhotoUri().build());


推荐阅读