英文:
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 "save project method"**
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***
******
**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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论