gpt4 book ai didi

android - Firebase 存储生成的 URL 不允许访问该文件

转载 作者:行者123 更新时间:2023-11-29 16:33:11 25 4
gpt4 key购买 nike

我无法在 Firebase 存储中检索我的图像,当我尝试访问图像的 Url 时,显示此消息:“X-Goog-Upload-Command header ,这不是有效的 X-谷歌文件”。我无法通过下载 URL 访问存储在存储中的文件。我寻求帮助,下面的代码是我如何将文件上传到存储和数据库!

    if (pmcUri != null){

// show the progress Dialog
pmcProgress = new ProgressDialog(this);
pmcProgress.setTitle("Creating your Feed");
pmcProgress.setMessage("...");
pmcProgress.show();

final StorageReference mChildStorage = pmcStorage.child("feedPhotos")
.child(pmcUri.getLastPathSegment());
mChildStorage.putFile(pmcUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
final Uri downloadUri = taskSnapshot.getUploadSessionUri();

FirebaseDatabase database = FirebaseDatabase.getInstance();


mChildStorage.getDownloadUrl();
// save infos in Database with user ID as a Reference
final DatabaseReference myRef = database.getReference("feeds").child(uid.getUid());
myRef.child("feed").setValue(setName);
assert downloadUri != null;
myRef.child("feedimg").setValue(downloadUri.toString());
myRef.child("feedkey").setValue(uid.getUid());


pmcProgress.dismiss();

Intent intent = new Intent(create_feed.this, glime.class);
startActivity(intent);
}
});

}

最佳答案

您读取 downloadUri 变量的方式是:

final Uri downloadUri = taskSnapshot.getUploadSessionUri();

调用 getUploadSessionUri() 会为您提供 upload session 的 URI,而不是文件上传完成后的下载 URL。任务快照不再提供下载 URL,而是需要对服务器进行额外的异步调用。

当我遇到问题时,我倾向于求助于 Firebase 文档,它提供了方便的 example of getting the download URL after an upload has completed :

final StorageReference ref = storageRef.child("images/mountains.jpg");
uploadTask = ref.putFile(file);

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 ref.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
} else {
// Handle failures
// ...
}
}
});

因此,为了在上传完成后获取下载 URL,您启动一​​个返回 ref.getDownloadUrl() 的新任务。

另见:

关于android - Firebase 存储生成的 URL 不允许访问该文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53652406/

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