当我使用firebase方法从firebase存储中检索下载URL时,它返回此格式“com.google.android.gms.tasks.zzu@41f55k88”[复制]

问题描述 投票:0回答:2

这个问题在这里已有答案:

每当我使用此方法在firebase存储taskSnapshot.getMetadata().getReference().getDownloadUrl().toString();中检索存储的图像url 我希望这个方法将返回已保存图像的下载URL,但它返回的是这种格式的东西com.google.android.gms.tasks.zzu@41f55k88这里是firebase数据库树enter image description here这里是我的代码片段。

final StorageReference filePath = userProfileImageRef.child(currentUserID + ".jpg");
            filePath.putFile(resultUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    final String downloadUrl = taskSnapshot.getMetadata().getReference().getDownloadUrl().toString();
                    Log.d("url", downloadUrl);
                    usersRef.child("profileimage").setValue(downloadUrl).addOnCompleteListener(new OnCompleteListener<Void>() {
                        @Override
                        public void onComplete(@NonNull Task<Void> task) {
                            if (task.isSuccessful()) {
                                Toast.makeText(SetupActivity.this, "Image stored", Toast.LENGTH_SHORT).show();
                            } else {
                                String message = task.getException().getMessage();
                                Toast.makeText(SetupActivity.this, "Error: " + message, Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }
            });
android firebase firebase-storage
2个回答
1
投票

尝试使用此方法

 filepath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                        @Override
                        public void onSuccess(Uri uri) {

                           final String downloadUrl = 
                           uri.toString();
                     }
                     });

所以它会

    final StorageReference filePath = userProfileImageRef.child(currentUserID + ".jpg");
                filePath.putFile(resultUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                      filepath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                            @Override
                            public void onSuccess(Uri uri) {

                               final String downloadUrl = 
                               uri.toString();
                         }
                         });
                    }


                        usersRef.child("profileimage").setValue(downloadUrl).addOnCompleteListener(new OnCompleteListener<Void>() {
                            @Override
                            public void onComplete(@NonNull Task<Void> task) {
                                if (task.isSuccessful()) {
                                    Toast.makeText(SetupActivity.this, "Image stored", Toast.LENGTH_SHORT).show();
                                } else {
                                    String message = task.getException().getMessage();
                                    Toast.makeText(SetupActivity.this, "Error: " + message, Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                    }
                });

0
投票

要做你想做的事我会做这样的事情:

我假设你有一个Uri与图像路径让我们称之为:filepath然后:

final String filename = filepath.getPath().substring(filepath.getPath().lastIndexOf("/")+1);
final StorageReference storagePath = mStorageRef.child("imagens/").child(filename);

storagePath.putFile(filepath).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
    @Override
    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
        storagePath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
            @Override
            public void onSuccess(Uri uri) {
                FileInfo fileInfo = new FileInfo(uri.toString(), filename);
                FirebaseDatabase.getInstance().getReference("The path in your database").push().setValue(fileInfo);
                Toast.makeText(UploadActivity.this, "Upload done", Toast.LENGTH_LONG).show();
            }
        });
    }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Toast.makeText(UploadActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
        }
    });

onSuccess方法有你想要的uri,我将它保存在类FileInfo中。

上传完成后,上传文件的信息存储在数据库中,您将检索如下信息:

mFirebaseInstance = FirebaseDatabase.getInstance();
mDatabase = mFirebaseInstance.getReference("The path in your database");
mDatabase.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        filesList.clear();
        for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
            FileInfo fileInfo = dataSnapshot1.getValue(FileInfo.class);
                filesList.add(fileInfo);
        }
    }
    @Override
    public void onCancelled(DatabaseError databaseError) {
        //Handle the mehtod in your way
    }
});

在上面的代码中,我将从数据库中检索您给出的给定路径下的所有“节点”,因为如果您有多于1个图像,您希望获取该URL,并且我将内容保存在我的FileInfo类中看起来像这样:

import android.net.Uri;

import java.io.Serializable;

public class FileInfo implements Serializable{

    String filename, downloadURL;

    public FileInfo(){
    }

    public FileInfo(String downloadURL, String filename) {
        this.downloadURL = downloadURL;
        this.filename = filename;
    }

    public String getDownloadURL() {
        return downloadURL;
    }

    public void setDownloadURL(String downloadURL) {
        this.downloadURL = downloadURL;
    }

    public String getFilename() {
        return filename;
    }

    public void setFilename(String filename) {
        this.filename = filename;
    }
}

使用Serialize实现的Firebase可以使用数据库的每个“节点”中存在的内容为您填写类。

注意:你不需要课程,但我认为它更适合你的问题,因为你在dabase中保存的不仅仅是图片网址,你可以根据自己的需要调整课程

© www.soinside.com 2019 - 2024. All rights reserved.