这个问题在这里已有答案:
每当我使用此方法在firebase存储taskSnapshot.getMetadata().getReference().getDownloadUrl().toString();
中检索存储的图像url
我希望这个方法将返回已保存图像的下载URL,但它返回的是这种格式的东西com.google.android.gms.tasks.zzu@41f55k88
这里是firebase数据库树这里是我的代码片段。
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();
}
}
});
}
});
尝试使用此方法
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();
}
}
});
}
});
要做你想做的事我会做这样的事情:
我假设你有一个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中保存的不仅仅是图片网址,你可以根据自己的需要调整课程