【问题标题】:Downloading Photo from Firebase Storage从 Firebase 存储下载照片
【发布时间】:2021-05-25 18:05:06
【问题描述】:

我已经尝试了所有我发现的方法,但仍然没有奏效,所以我在这里写

我正在尝试从我的文件夹“images”中从 Firebase 存储下载照片,该文件夹将是与当前授权用户 ID 同名的文件。这里唯一的问题是它不起作用。仅显示对象的背景(我正在更改 ShapableImageView 上的照片)

下面我从应用中添加代码、日志和照片。

代码:

  private void downloadProfile() {

    User localUser = new User("","","0","","",0,0,0);
    if(mAuth.getCurrentUser()!=null){
        database_ref.child("users").child(mAuth.getCurrentUser().getUid()).addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                if(snapshot.exists()){
                    localUser.name = snapshot.child("name").getValue().toString();
                    localUser.mail = mAuth.getCurrentUser().getEmail();
                    localUser.age = snapshot.child("age").getValue().toString();
                    localUser.phone = snapshot.child("phone").getValue().toString();
                    localUser.address = snapshot.child("address").getValue().toString();
                    localUser.login_method = Integer.parseInt(snapshot.child("login_method").getValue().toString());
                    localUser.playlist_amount = Integer.parseInt(snapshot.child("playlist_amount").getValue().toString());
                    localUser.fav_song_amount = Integer.parseInt(snapshot.child("fav_song_amount").getValue().toString());

                    profile_email.setText(localUser.mail);
                    profile_name.setText( localUser.name);
                    age_edit_t.setText(localUser.age);
                    phone_edit_t.setText(localUser.phone);
                    address_edit_t.setText(localUser.address);

                    //  Image download
                    storageReference = storage.getReferenceFromUrl("gs://altas-notas.appspot.com");

                    storageReference.child("images/"+mAuth.getCurrentUser().getUid()+".jpg").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                        @Override
                        public void onSuccess(Uri uri) {
                            profile_img.setImageURI(uri);
                        }
                    }).addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception exception) {
                            // Handle any errors
                        }
                    });

               
                }
            }

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

            }
        });
    }
}

日志:https://pastebin.com/F0hCxZsv

无论我如何尝试下载并将照片设置为透明或默认。

如果重要的话,我还会添加部分更改图像的代码:

    private void startGallery() {
    Intent cameraIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    cameraIntent.setType("image/*");
    if (cameraIntent.resolveActivity(getActivity().getPackageManager()) != null) {
        startActivityForResult(cameraIntent, 1000);
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    //super method removed
    if (resultCode == RESULT_OK) {

        if (requestCode == 1000) {
             returnUri = data.getData();
            Bitmap bitmapImage = null;
            try {
                bitmapImage = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), returnUri);
                profile_img.setImageBitmap(bitmapImage);
            } catch (IOException e) {
                e.printStackTrace();
            }


            //Upload image

            storageReference = FirebaseStorage.getInstance().getReference();
            storageReference.child("images/"+mAuth.getCurrentUser().getUid()).putFile(returnUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
                    if(task.isSuccessful()){
                        System.out.println("Upload image is successful!");

                    }else{
                        System.out.println("Upload image failed!");

                    }
                }
            });
            //Upload rest of information
            updateProfile();
        }
    }

}

【问题讨论】:

    标签: android firebase storage


    【解决方案1】:

    在您的代码中:

    storageReference.child("images/"+mAuth.getCurrentUser().getUid()+".jpg")
    

    您不需要.jpg。试试这样:

    storageReference.child("images/"+mAuth.getCurrentUser().getUid())
    

    【讨论】:

    • 我之前也试过这个,它没有用。我忘了关闭它,但我找到了帮助我加载照片的 Piccasso 和 Glide。我更喜欢 Glide 进行高速加载
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-29
    • 1970-01-01
    • 2022-11-15
    • 1970-01-01
    • 2021-07-04
    • 2016-12-07
    相关资源
    最近更新 更多