首页 > 解决方案 > 如何返回第二个值

问题描述

关于如何在此代码中返回第二个值的任何想法?

private void uploadImage_10() {
    final ProgressDialog pd = new ProgressDialog(this);
    pd.setMessage("Posting");
    pd.show();
    if (mImageUri != null&&mImageUri2 != null){
        final StorageReference fileReference = storageRef.child(System.currentTimeMillis()
                + "." + getFileExtension(mImageUri));
        final StorageReference fileReference2 = storageRef.child(System.currentTimeMillis()
                + "." + getFileExtension(mImageUri2));

        uploadTask2 = fileReference2.putFile(mImageUri2);
        uploadTask = fileReference.putFile(mImageUri);
        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();
                }
                return fileReference.getDownloadUrl();
            }
        }).addOnCompleteListener(new OnCompleteListener<Uri>() {
            @Override
            public void onComplete(@NonNull Task<Uri> task) {
                if (task.isSuccessful()) {
                    Uri downloadUri = task.getResult();
                    miUrlOk = downloadUri.toString();
                    Uri downloadUri2 = task.getResult();
                    miUrlOk2 = downloadUri2.toString();

                    DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Posts");

                    String postid = reference.push().getKey();

                        HashMap<String, Object> hashMap = new HashMap<>();
                        hashMap.put("postid", postid);
                        hashMap.put("postimage", miUrlOk);
                        hashMap.put("postimage2", miUrlOk2);
                        hashMap.put("description", description.getText().toString());
                        hashMap.put("publisher", FirebaseAuth.getInstance().getCurrentUser().getUid());

我想返回 fileReference.getDownloadUrl(); 和 fileReference2.getDownloadUrl(); 但不知道如何。

标签: javaandroidfirebasefirebase-realtime-databasefirebase-storage

解决方案


除非您在 List 中传递它们,否则您不能在一个方法中返回两个值。

当前的要求可以通过在单独的方法中传递 StorageReference 来实现,如下所示。

private void continueWithTask(StorageReference  reference){
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();
                }
                return reference;
            }
        }

稍后像这样调用他们 continueWithTask(fileReference) & continueWithTask(fileReference2 )


推荐阅读