我正在尝试设置一个完整的侦听器,以查找图像是否已成功上传到Firebase。但是,当我尝试上传图像时,应用程序崩溃并显示此错误。相同的代码在我使用它之前就可以使用,但是现在不行了。 java.lang.UnsupportedOperationException:未实现addOnCompleteListener
这是我使用的代码:
upload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (uriImage != null) {
final String imageName = UUID.randomUUID().toString();
StorageReference child_location = storeRef.child("images/" + userId + "/" + imageName);
child_location.putFile(uriImage).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
if (task.isSuccessful()) {
Toast.makeText(secondActivity.this, "upload successful.", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(secondActivity.this, "upload failed.", Toast.LENGTH_SHORT).show();
task.getException();
}
}
});
final StorageReference ref_restored = storeRef.child("restored_images/" + userId + "/" + imageName);
UploadTask uploadTask = ref_restored.putFile(uriImage);
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_restored.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
databaseReference.child(userId).child("restored_images_url").child(imageName).setValue(downloadUri.toString());
} else {
// Handle failures
// ...
}
}
});
}
}
});
该代码应上载先前选择的图像。但是现在整个应用程序崩溃了,这是显示的完整错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.adityaaravi.project2, PID: 5679
java.lang.UnsupportedOperationException: addOnCompleteListener is not implemented
at com.google.android.gms.tasks.Task.addOnCompleteListener(Unknown Source:4)
at com.example.adityaaravi.project2.secondActivity$3.onClick(secondActivity.java:85)
at android.view.View.performClick(View.java:6259)
at android.view.View$PerformClick.run(View.java:24732)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6695)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:772)
I/DynamiteModule: Considering local module com.google.android.gms.firebasestorage:0 and remote module com.google.android.gms.firebasestorage:6
I/DynamiteModule: Selected remote version of com.google.android.gms.firebasestorage, version >= 6
W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
Application terminated.
我用来声明Firebase库的build.gradle代码:
implementation 'com.google.firebase:firebase-core:9.6.1'
implementation 'com.google.firebase:firebase-auth:9.6.1'
implementation 'com.google.firebase:firebase-storage:9.6.1'
implementation 'com.google.firebase:firebase-database:9.6.1'
implementation 'com.google.firebase:firebase-messaging:9.6.1'