无法在Firebase上实现多图上传的 addOnSuccessListener,即进入 addOnFailureListener。

huangapple go评论73阅读模式
英文:

not working addOnSuccessListener for multiple image uploading on firebase. i.e. entering in addOnFailureListener

问题

private void saveProject() {

    String pro_name, client_name, pro_rec_date, pro_comp_date, total_views, pro_no, arch_no;

    pro_no = binding.projectNo.getText().toString();
    arch_no = binding.architectNumber.getText().toString();
    pro_name = binding.projectName.getText().toString();
    client_name = binding.ClientName.getText().toString();
    pro_comp_date = binding.projectCompleteDate.getText().toString();
    pro_rec_date = binding.projectReceivedDate.getText().toString();
    total_views = binding.totalViewsOfPhotos.getText().toString();

    progressDialog.setMessage("Writing in Database...");
    progressDialog.setCancelable(false);
    progressDialog.setIcon(R.mipmap.ic_launcher_round);
    progressDialog.setTitle("Aurum Design");
    progressDialog.show();

    if (!(item.equals("Payment Received or Not?") && item2.equals("Project Status"))) {

        reference = FirebaseDatabase.getInstance().getReference("project_details");
        StorageReference ImageFolder = FirebaseStorage.getInstance().getReference().child("ProjectImages");
        final HashMap<String, Object> hashMap = new HashMap<>();

        //upload data
        hashMap.put("architect_no", arch_no);
        hashMap.put("client_name", client_name);
        hashMap.put("payment_received", item);
        hashMap.put("project_status", item2);
        hashMap.put("project_complete_date", pro_comp_date);
        hashMap.put("project_name", pro_name);
        hashMap.put("project_no", pro_no);
        hashMap.put("project_received_date", pro_rec_date);
        hashMap.put("total_views_of_photos", total_views);

        //upload Images
        for (upload_count = 0; upload_count < uri.size(); upload_count++) {
            Uri IndividualImage = uri.get(upload_count);
            final StorageReference ImageName = ImageFolder.child("Image" + pro_name + IndividualImage.getLastPathSegment());

            ImageName.putFile(IndividualImage).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    ImageName.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                        @Override
                        public void onSuccess(Uri uri) {
                            String URL = String.valueOf(uri);
                            hashMap.put("image_url" + upload_count, URL);
                        }
                    });
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Toast.makeText(activity, "failed", Toast.LENGTH_SHORT).show();
                }
            });
        }

        reference.push().setValue(hashMap);
        uri.clear();
        progressDialog.dismiss();
        startActivity(new Intent(activity, MainActivity.class));
        activity.finish();

    } else {
        progressDialog.dismiss();
        Toast.makeText(activity, "select payment received or not?", Toast.LENGTH_SHORT).show();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(activity, "select Project Status", Toast.LENGTH_SHORT).show();
            }
        }, 1000);
    }
}

in the upper code addOnSuccessListener is not working

ImageName.putFile(IndividualImage).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
    @Override
    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
        ImageName.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
            @Override
            public void onSuccess(Uri uri) {
                String URL = String.valueOf(uri);
                hashMap.put("image_url" + upload_count, URL);
            }
        });
    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception e) {
        Toast.makeText(activity, "failed", Toast.LENGTH_SHORT).show();
    }
});

please anyone knows the solution, please tell me. THANKS IN ADVANCE...


<details>
<summary>英文:</summary>
I want to upload multiple images on firebase storege. but I am not able to upload it.
Below is my code.
******
**AddNewProjectActivity.java &quot;save project method&quot;**

private void saveProject() {

    String pro_name, client_name, pro_rec_date, pro_comp_date, total_views, pro_no, arch_no;
pro_no = binding.projectNo.getText().toString();
arch_no = binding.architectNumber.getText().toString();
pro_name = binding.projectName.getText().toString();
client_name = binding.ClientName.getText().toString();
pro_comp_date = binding.projectCompleteDate.getText().toString();
pro_rec_date = binding.projectReceivedDate.getText().toString();
total_views = binding.totalViewsOfPhotos.getText().toString();
progressDialog.setMessage(&quot;Writing in Database...&quot;);
progressDialog.setCancelable(false);
progressDialog.setIcon(R.mipmap.ic_launcher_round);
progressDialog.setTitle(&quot;Aurum Design&quot;);
progressDialog.show();
if (!(item.equals(&quot;Payment Received or Not?&quot;) &amp;&amp; item2.equals(&quot;Project Status&quot;))) {
reference = FirebaseDatabase.getInstance().getReference(&quot;project_details&quot;);
StorageReference ImageFolder = FirebaseStorage.getInstance().getReference().child(&quot;ProjectImages&quot;);
final HashMap&lt;String, Object&gt; hashMap = new HashMap&lt;&gt;();
//upload data
hashMap.put(&quot;architect_no&quot;, arch_no);
hashMap.put(&quot;client_name&quot;, client_name);
hashMap.put(&quot;payment_received&quot;, item);
hashMap.put(&quot;project_status&quot;, item2);
hashMap.put(&quot;project_complete_date&quot;, pro_comp_date);
hashMap.put(&quot;project_name&quot;, pro_name);
hashMap.put(&quot;project_no&quot;, pro_no);
hashMap.put(&quot;project_received_date&quot;, pro_rec_date);
hashMap.put(&quot;total_views_of_photos&quot;, total_views);
//upload Images
for (upload_count = 0; upload_count &lt; uri.size(); upload_count++) {
Uri IndividualImage = uri.get(upload_count);
final StorageReference ImageName = ImageFolder.child(&quot;Image&quot; + pro_name + IndividualImage.getLastPathSegment());
ImageName.putFile(IndividualImage).addOnSuccessListener(new OnSuccessListener&lt;UploadTask.TaskSnapshot&gt;() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
ImageName.getDownloadUrl().addOnSuccessListener(new OnSuccessListener&lt;Uri&gt;() {
@Override
public void onSuccess(Uri uri) {
String URL = String.valueOf(uri);
hashMap.put(&quot;image_url&quot;+upload_count,URL);
}
});
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(activity, &quot;failed&quot;, Toast.LENGTH_SHORT).show();
}
});
}
reference.push().setValue(hashMap);
uri.clear();
progressDialog.dismiss();
startActivity(new Intent(activity, MainActivity.class));
activity.finish();
} else {
progressDialog.dismiss();
Toast.makeText(activity, &quot;select payment received or not?&quot;, Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Toast.makeText(activity, &quot;select Project Status&quot;, Toast.LENGTH_SHORT).show();
}
}, 1000);
}
}
******
***in the upper code addOnSuccessListener is not working***
******
**entering in addOnFailureListener**

ImageName.putFile(IndividualImage).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
ImageName.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
String URL = String.valueOf(uri);
hashMap.put("image_url"+upload_count,URL);
}
});
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(activity, "failed", Toast.LENGTH_SHORT).show();
}
});

******
please anyone knows the solution, please tell me. THANKS IN ADVANCE...
</details>
# 答案1
**得分**: 0
我解决了这个错误。我忘记将存储规则更新为公开。
****
上面的代码是没有错误的代码...所以,你可以使用这段代码来在Firebase存储上上传多张图片。***但是,不要忘记检查Firebase规则***,我在下面添加了规则。
****
**Firebase存储规则**
```firebase存储规则
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write;
}
}
}```
<details>
<summary>英文:</summary>
**I solved the error. I forgot to update storage rules to public.**
****
upper code is error free code... so, you can use this code for upload multiple images on firebase storage. ***but, dont forget to check the firebase rules***, I am adding rules below.
****
**firebase storage rules**

rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write;
}
}
}


</details>

huangapple
  • 本文由 发表于 2020年10月17日 17:08:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/64400801.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定