我无法从 firebase 获取 getdownloadurl()。请任何人帮助这是我的代码和错误。 (我正在使用片段)
I am not able to getdownloadurl() from firebase. Please anybody help here's my code and error. (I am using Fragment)
这是我的代码:
private void uploadFile() {
//checking if file is available
if (filePath != null) {
//displaying progress dialog while image is uploading
final ProgressDialog progressDialog = new ProgressDialog(getContext());
progressDialog.setTitle("Uploading");
progressDialog.show();
//getting the storage reference
StorageReference sRef = storageReference.child(Constants.STORAGE_PATH_UPLOADS + System.currentTimeMillis() + "."
+ getFileExtension(filePath));
UploadTask uploadTask = storageReference.putFile(filePath);
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 sRef.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
Postmember postmember = new Postmember();
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
postmember.setType("iv");
String id = mDatabase.push().getKey();
mDatabase.child(id).setValue(postmember);
} else {
// Handle failures
// ...
}
}
});
}
}
这是我的错误:
E/StorageException: StorageException has occurred.
An unknown error occurred, please check the HTTP result code and inner exception for server response.
Code: -13000 HttpResult: 0
E/StorageException: 无法上传到 getRoot。您应该上传到存储位置,例如 .getReference('image.png').putFile...
java.lang.IllegalArgumentException: 无法上传到 getRoot。您应该上传到存储位置,例如 .getReference('image.png').putFile...
在 com.google.firebase.storage.UploadTask.run(UploadTask.java:213)
在 com.google.firebase.storage.StorageTask.lambda$getRunnable$7$StorageTask(StorageTask.java:1072)
在 com.google.firebase.storage.-$$Lambda$StorageTask$q9YBoR_A8LB-JxTCx8JRQvabaZs.run(未知 Source:2)
在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
在 java.lang.Thread.run(Thread.java:923)
出现以下错误:
E/StorageException: Cannot upload to getRoot. You should upload to a storage location such as .getReference('image.png').putFile
说的很清楚是什么问题。如果没有名称和文件扩展名,您无法将文件直接上传到根目录。所以给你带来麻烦的那一行是:
UploadTask uploadTask = storageReference.putFile(filePath);
没有看到 storageReference
对象是如何定义的,我可以说它很可能指向根目录。要解决问题,您应该将上面的代码行更改为:
StorageReference sRef = storageReference.child(Constants.STORAGE_PATH_UPLOADS + System.currentTimeMillis() + "."
+ getFileExtension(filePath));
UploadTask uploadTask = sRef.putFile(filePath);
所以应该使用的正确对象是 sRef
,因为它指向正确的路径并且 而不是 storageReference
.
这是我的代码:
private void uploadFile() {
//checking if file is available
if (filePath != null) {
//displaying progress dialog while image is uploading
final ProgressDialog progressDialog = new ProgressDialog(getContext());
progressDialog.setTitle("Uploading");
progressDialog.show();
//getting the storage reference
StorageReference sRef = storageReference.child(Constants.STORAGE_PATH_UPLOADS + System.currentTimeMillis() + "."
+ getFileExtension(filePath));
UploadTask uploadTask = storageReference.putFile(filePath);
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 sRef.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
Postmember postmember = new Postmember();
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
postmember.setType("iv");
String id = mDatabase.push().getKey();
mDatabase.child(id).setValue(postmember);
} else {
// Handle failures
// ...
}
}
});
}
}
这是我的错误:
E/StorageException: StorageException has occurred.
An unknown error occurred, please check the HTTP result code and inner exception for server response.
Code: -13000 HttpResult: 0
E/StorageException: 无法上传到 getRoot。您应该上传到存储位置,例如 .getReference('image.png').putFile... java.lang.IllegalArgumentException: 无法上传到 getRoot。您应该上传到存储位置,例如 .getReference('image.png').putFile... 在 com.google.firebase.storage.UploadTask.run(UploadTask.java:213) 在 com.google.firebase.storage.StorageTask.lambda$getRunnable$7$StorageTask(StorageTask.java:1072) 在 com.google.firebase.storage.-$$Lambda$StorageTask$q9YBoR_A8LB-JxTCx8JRQvabaZs.run(未知 Source:2) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 在 java.lang.Thread.run(Thread.java:923)
出现以下错误:
E/StorageException: Cannot upload to getRoot. You should upload to a storage location such as .getReference('image.png').putFile
说的很清楚是什么问题。如果没有名称和文件扩展名,您无法将文件直接上传到根目录。所以给你带来麻烦的那一行是:
UploadTask uploadTask = storageReference.putFile(filePath);
没有看到 storageReference
对象是如何定义的,我可以说它很可能指向根目录。要解决问题,您应该将上面的代码行更改为:
StorageReference sRef = storageReference.child(Constants.STORAGE_PATH_UPLOADS + System.currentTimeMillis() + "."
+ getFileExtension(filePath));
UploadTask uploadTask = sRef.putFile(filePath);
所以应该使用的正确对象是 sRef
,因为它指向正确的路径并且 而不是 storageReference
.