我正在尝试获取上传到 firebase 数据库的图像的下载网址。但任务Uri imageURL = storageReference.getDownloadUrl();
没有给出存储在 firebase 存储中的图像的实际下载 URL,即它给出了这个 - com.google.android.gms.tasks.zzu@27da5837
getdownloadUrl()
在以下情况下不起作用,因为它已被弃用:
Uri imageUrl = storagereference.getdownloadUrl(); //给出错误
请参阅此处的代码:
final StorageReference storageReference = FirebaseStorage.getInstance().getReference().child("images").child(imageName);
final UploadTask uploadTask = storageReference.putBytes(data);
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle unsuccessful uploads
Toast.makeText(CreateSnapActivity.this, "Upload Failed", Toast.LENGTH_SHORT).show();
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(final UploadTask.TaskSnapshot taskSnapshot) {
// taskSnapshot.getMetadata() contains file metadata such as size, content-type, etc.
//Task<Uri> imageURL = storageReference.getDownloadUrl();
//Log.i("URL", imageURL.toString()); // gives this url - com.google.android.gms.tasks.zzu@27da5837 which is not correct
Task<Uri> urlTask = storageReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
Log.i("Url", String.valueOf(uri));
imageUrl = uri.toString(); //Gives the correct url but it cannot store this uri value outside this function i.e for Intent shown below outside this func.
}
});
Toast.makeText(CreateSnapActivity.this, "Uploaded", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(CreateSnapActivity.this, ChooseUserActivity.class);
intent.putExtra("imageName", imageName);
intent.putExtra("imageURL", imageUrl);
intent.putExtra("message", captionEditText.getText().toString());
startActivity(intent);
}
});
Calling getDownloadUrl()
启动异步操作,这可能需要一些时间才能完成。因此,在检索下载 URL 时,您的主代码会继续运行,以防止阻止用户。然后当下载 URL 可用时,您的onSuccess
被叫。正因为如此,当你的intent.putExtra("imageURL", imageUrl)
现在运行,imageUrl = uri.toString()
还没跑。
为了证明这确实是正确的,我建议在代码中添加一些日志记录以仅显示流程,或者在调试器中运行它并在我上面指出的行上设置断点。
要修复此问题,任何需要下载 URL 的代码都需要位于onSuccess
,或者从那里被调用。这不仅适用于此处,还适用于所有异步运行的代码,其中包括大多数现代云 API。因此,我建议您现在花一些时间研究这种行为,以便您以后能更适应它。
在你的代码中:
final StorageReference storageReference = FirebaseStorage.getInstance().getReference().child("images").child(imageName);
final UploadTask uploadTask = storageReference.putBytes(data);
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
Toast.makeText(CreateSnapActivity.this, "Upload Failed", Toast.LENGTH_SHORT).show();
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(final UploadTask.TaskSnapshot taskSnapshot) {
Task<Uri> urlTask = storageReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
imageUrl = uri.toString();
Toast.makeText(CreateSnapActivity.this, "Uploaded", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(CreateSnapActivity.this, ChooseUserActivity.class);
intent.putExtra("imageName", imageName);
intent.putExtra("imageURL", imageUrl);
intent.putExtra("message", captionEditText.getText().toString());
startActivity(intent);
}
});
}
});
另请参阅:
- Firebase Storage 生成的 URL 不允许访问该文件 https://stackoverflow.com/questions/53652406/url-generated-by-firebase-storage-does-not-allow-access-to-the-file/53653522#53653522
- 如何获取 firebase 存储的下载 url 并更新 firestore 文档? https://stackoverflow.com/questions/62699565/how-to-get-download-url-for-firebase-storage-and-update-firestore-document/62699990#62699990
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)