这个问题在这里已有答案:
我正在从存储在firebase存储中的歌曲文件中检索下载URL。我获取的URL与firebase存储中的URL不同。
这是我得到的错误链接:com.google.android.gms.tasks.zzu@75f559a
这是我的数据库设置图片的链接:https://imgur.com/a/Gtl1ThZ
这是我的代码:
final String fileName = songUri.getLastPathSegment() + "";
//final String fileName1=songUri.getLastPathSegment()+"";
final StorageReference storageRef = storage.getReference();
storageRef.child("Uploads").child(fileName).putFile(songUri)
.addOnSuccessListener(new
OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
String url =
storageRef.child("Uploads").child(fileName).getDownloadUrl().toString();
//returns the url of the uploaded file
DatabaseReference reference = database.getReference();
reference.child("Uploads").child(fileName).setValue(url).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful())
Toast.makeText(Upload.this, "File Uploaded Successfully", Toast.LENGTH_SHORT).show();
else
Toast.makeText(Upload.this, "Upload failed", Toast.LENGTH_SHORT).show();
}
});
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(Upload.this, "Upload failed",Toast.LENGTH_SHORT).show();
}
storageRef.child("Uploads").child(fileName).getDownloadUrl();
返回一个Task对象,而不是直接返回Uri。您必须向此任务添加完成侦听器,然后将该URL上载到您的数据库。
final String fileName = songUri.getLastPathSegment() + "";
//final String fileName1=songUri.getLastPathSegment()+"";
final StorageReference storageRef = storage.getReference();
storageRef.child("Uploads").child(fileName).putFile(songUri)
.addOnSuccessListener(new OnSuccessListener < UploadTask.TaskSnapshot > () {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
storageRef.child("Uploads").child(fileName).getDownloadUrl()
.addOnCompleteListener(new OnCompleteListener < Uri > () {
@Override
public void onComplete(@NonNull Task < Uri > task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
reference.child("Uploads").child(fileName).setValue(downloadUri.toString()).addOnCompleteListener(new OnCompleteListener < Void > () {
@Override
public void onComplete(@NonNull Task < Void > task) {
if (task.isSuccessful())
Toast.makeText(Upload.this, "File Uploaded Successfully", Toast.LENGTH_SHORT).show();
else
Toast.makeText(Upload.this, "Upload failed", Toast.LENGTH_SHORT).show();
}
});
} else {
Toast.makeText(Upload.this, "upload failed: " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(Upload.this, "Upload failed", Toast.LENGTH_SHORT).show();
}