gpt4 book ai didi

java - getDownloadUrl 函数未返回正确的 url

转载 作者:行者123 更新时间:2023-12-02 02:06:33 24 4
gpt4 key购买 nike

我正在将 pdf 上传到 Firebase Storage,并希望获取其下载网址。我正在使用以下代码:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 1212:
if (resultCode == RESULT_OK) {
String FilePath = data.getDataString();
t.setText(FilePath);
Uri pdfuri = data.getData();
final StorageReference mp = mpdf.child("pdf").child(random()+".pdf");
mp.putFile(pdfuri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
if(task.isSuccessful())
{
String download_url = task.getResult().getMetadata().getReference().getDownloadUrl().toString();
String parent = getIntent().getStringExtra("type").toString();
String ch = getIntent().getStringExtra("value").toString();
GeologicDatabase.modifySingle(parent,ch,download_url);
Toast.makeText(modifypdf.this,download_url , Toast.LENGTH_SHORT).show();
finish();
}
else {
Toast.makeText(modifypdf.this,"Faild" , Toast.LENGTH_SHORT).show();
}
}
});
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}

该行的结果

String download_url = task.getResult().getMetadata().getReference().getDownloadUrl().toString();

是“com.google.android.gms.tasks.zzu@7f93358”网址不对

<小时/>

问题的解决方案是使用上传任务,并在其完整监听器中编写代码,以 Uri 形式获取任务结果并将其转换为字符串

final String name = random() + ".pdf";
dp = mpdf.child("pdf").child(name);
UploadTask uploadTask = dp.putFile(pdfuri);
Task<Uri> urlTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
@Override
public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
if (!task.isSuccessful()) {
throw task.getException();
}

// Continue with the task to get the download URL
return dp.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
tt = title.getText().toString();
Uri downloadUri = task.getResult();
String download_url = downloadUri.toString();
String parent = getIntent().getStringExtra("type").toString();
String ch = getIntent().getStringExtra("value").toString();
GeologicDatabase.updateChild(parent, ch, "url",download_url,tt);
GeologicDatabase.updateChild(parent,ch,"name",name,tt);
Toast.makeText(addpdf.this, download_url, Toast.LENGTH_SHORT).show();
finish();
} else {
// Handle failures
// ...
}
}
});

最佳答案

getDownloadUrl()不返回 URL。从the javadoc可以看出,它返回一个产生 URL 的任务。您必须等待此任务完成才能获取最终 URL。

关于java - getDownloadUrl 函数未返回正确的 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50685756/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com