Android 从Firebase存储下载照片

Android 从Firebase存储下载照片,android,firebase,storage,Android,Firebase,Storage,我已经尝试了我发现的每一种方法,但仍然不起作用,所以我在这里写作 我正试图从我的文件夹“images”中从Firebase存储下载照片,其中的文件名将与当前授权用户ID相同。这里唯一的问题是它不起作用。仅显示对象的背景(我正在ShapableMageView上更改照片) 下面我从应用程序中添加代码、日志和照片 代码: private void downloadProfile() { User localUser = new User("",""

我已经尝试了我发现的每一种方法,但仍然不起作用,所以我在这里写作

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

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

代码:

  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) {

            }
        });
    }
}
private void downloadProfile(){
用户localUser=新用户(“,”,“0”,“,”,“,”,0,0,0);
if(mAuth.getCurrentUser()!=null){
数据库_ref.child(“用户”).child(mAuth.getCurrentUser().getUid()).addListenerForSingleValueEvent(新值EventListener()){
@凌驾
public void onDataChange(@NonNull DataSnapshot snapshot 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(“地址”).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);
年龄编辑设置文本(localUser.age);
phone\u edit\u t.setText(localUser.phone);
地址\u编辑\u t.setText(localUser.address);
//图像下载
storageReference=storage.getReferenceFromUrl(“gs://altas notas.appspot.com”);
storageReference.child(“images/”+mAuth.getCurrentUser().getUid()+“.jpg”).getDownloadUrl().addOnSuccessListener(新OnSuccessListener()){
@凌驾
成功时的公共无效(Uri){
配置文件\u img.setImageURI(uri);
}
}).addOnFailureListener(新的OnFailureListener(){
@凌驾
public void onFailure(@NonNull异常){
//处理任何错误
}
});
}
}
@凌驾
已取消公共void(@NonNull DatabaseError){
}
});
}
}
日志:

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

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

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

}
private void startGallery(){
Intent-cameraIntent=新的Intent(Intent.ACTION\u PICK,MediaStore.Images.Media.EXTERNAL\u CONTENT\u URI);
cameraIntent.setType(“image/*”);
if(cameraIntent.resolveActivity(getActivity().getPackageManager())!=null){
startActivityForResult(Camerainent,1000);
}
}
@凌驾
ActivityResult上的公共void(int请求代码、int结果代码、意图数据){
//超级方法删除
if(resultCode==RESULT\u OK){
if(requestCode==1000){
returnUri=data.getData();
位图bitmapImage=null;
试一试{
bitmapImage=MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(),returnUri);
配置文件\u img.setImageBitmap(位图图像);
}捕获(IOE异常){
e、 printStackTrace();
}
//上传图像
storageReference=FirebaseStorage.getInstance().getReference();
storageReference.child(“images/”+mAuth.getCurrentUser().getUid()).putFile(returnUri).addOnCompleteListener(新的OnCompleteListener()){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
System.out.println(“上传图像成功!”);
}否则{
System.out.println(“上传图像失败!”);
}
}
});
//上传其余信息
updateProfile();
}
}
}
在您的代码中:

storageReference.child(“images/”+mAuth.getCurrentUser().getUid()+“.jpg”)
您不需要
.jpg
。试着这样做:

storageReference.child(“images/”+mAuth.getCurrentUser().getUid())

我以前也试过这个,但没用。我忘了关上它,但我找到了Piccaso和Glide,这帮我加载了照片。我更喜欢滑翔高速装载