【问题标题】:How to return a second value [duplicate]如何返回第二个值[重复]
【发布时间】:2020-01-08 14:18:18
【问题描述】:

知道如何在这段代码中返回第二个值吗?

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();但不知道怎么做。

【问题讨论】:

  • 如果问题是指IDE Android-Studio,请仅使用android-studio标签。
  • 是自定义方法吗,我的意思是你定义的方法?如果是,那么您可以将返回类型更改为 List 或 Array 或自定义 Object。

标签: java android firebase firebase-realtime-database firebase-storage


【解决方案1】:

你不能在一个方法中返回两个值,除非你在 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 )

【讨论】:

  • 除了使用列表之外,还有其他选项。您可以使用其他集合类型、数组、自定义对象、Map.Entry 等。
猜你喜欢
  • 2019-10-15
  • 1970-01-01
  • 2018-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-12
  • 2014-12-17
相关资源
最近更新 更多