I am uploading a pdf to Firebase Storage and want to get its download url. I am using the following code:
@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);
}
the result of the line
String download_url = task.getResult().getMetadata().getReference().getDownloadUrl().toString();
is "com.google.android.gms.tasks.zzu@7f93358" not the right url
the solution of the problem is to use upload task and in its in Complete Listener you write your code get the result of the task as a Uri and cast it into String
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
// ...
}
}
});
答案 0 :(得分:1)
getDownloadUrl() doesn't return a URL. As you can see from the javadoc, it returns a Task that yields the URL. You'll have to wait on this Task to complete in order to get the final URL.
答案 1 :(得分:1)
//尝试这个
ret=output_excel.rename(columns={"Home":str(self.hhid)+" Home","HH1 distance [km]":str(self.hhid)+" Distance Car1 [km]", "Energy [kWh]":str(self.hhid)+" Energy Car1 [kWh]"})
return ret