android - getDownloadUrl() 方法在 firebase 更新后被弃用
问题描述
getDownloadUrl()
firebase 更新后不推荐使用该方法。这种方法的替代代码是什么?
public void createOrUpdatePostWithImage(Uri imageUri, final OnPostCreatedListener onPostCreatedListener, final Post post) {
// Register observers to listen for when the download is done or if it fails
DatabaseHelper databaseHelper = ApplicationHelper.getDatabaseHelper();
if (post.getId() == null) {
post.setId(generatePostId());
}
final String imageTitle = ImageUtil.generateImageTitle(UploadImagePrefix.POST, post.getId());
UploadTask uploadTask = databaseHelper.uploadImage(imageUri, imageTitle);
if (uploadTask != null) {
uploadTask.addOnFailureListener(exception -> {
// Handle unsuccessful uploads
onPostCreatedListener.onPostSaved(false);
}).addOnSuccessListener(taskSnapshot -> {
// taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL.
Uri downloadUrl = taskSnapshot.getDownloadUrl();
LogUtil.logDebug(TAG, "successful upload image, image url: " + String.valueOf(downloadUrl));
post.setImagePath(String.valueOf(downloadUrl));
post.setImageTitle(imageTitle);
createOrUpdatePost(post);
onPostCreatedListener.onPostSaved(true);
});
}
}
解决方案
使用此代码获取 ImagesUrl。
我希望它有效!
我在 github 上也有一个存储库,这是链接。 https://github.com/BVLD/Firebase-Storage-upload-and-retreive-images-android-studio
ImagesPath.putFile(mainImageURI).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();
}
return ImagesPath.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downUri = task.getResult();
downUri.toString();