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

时间:2018-08-31 22:21:15

标签: android firebase firebase-storage

每当我使用此方法检索Firebase存储中存储的图像URL时 taskSnapshot.getMetadata().getReference().getDownloadUrl().toString();
我希望该方法将返回已保存图像的下载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();
                            }
                        }
                    });
                }
            });

2 个答案:

答案 0 :(得分:0)

尝试使用此方法

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

答案 1 :(得分: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中保存不仅仅是图像url的方式,这样您就可以使类适应您的需求