Hi all,
I've been struggling for days to figure out a solution for this.
I am using BSImagePicker for multi image selection along side of a form.
So the user chooses images from storage, fill out a form then submit. I limited the number of images to 6.
Everything works fine when all 6 images were selected, but if the user decides to upload less than 6 the app crashes.
Full class: https://pastebin.com/F8y7esud
filepath.putFile(mSelectedUri).addOnSuccessListener
filepath1.putFile(mSelectedUri1).addOnSuccessListener
filepath2.putFile(mSelectedUri2).addOnSuccessListener
filepath3.putFile(mSelectedUri3).addOnSuccessListener
filepath4.putFile(mSelectedUri4).addOnSuccessListener
filepath5.putFile(mSelectedUri5).addOnSuccessListener
If a user selects 2 images then Logcat points to filepath2
If they selects 3 Logcat points to path3 (uri cannot be null)
How do I check for NULL in this situation?
The thing is if I check it like this:
If (mSelectedUri3 != null ) { the rest of the code }
and the user uploads/selects only 3 images then mSelectedUri3 is null and it won't execute it. Just stops there.
if (mAllUri != null ) {
Toast.makeText(getActivity(), "uploading image", Toast.LENGTH_SHORT).show();
final String postId = FirebaseDatabase.getInstance().getReference().push().getKey();
StorageReference filepath = FirebaseStorage.getInstance().getReference()
.child("posts/users/" + FirebaseAuth.getInstance().getCurrentUser().getUid() +
"/" + postId + "/post_image");
final StorageReference filepath1 = FirebaseStorage.getInstance().getReference()
.child("posts/users/" + FirebaseAuth.getInstance().getCurrentUser().getUid() +
"/" + postId + "/post_image1");
final StorageReference filepath2 = FirebaseStorage.getInstance().getReference()
.child("posts/users/" + FirebaseAuth.getInstance().getCurrentUser().getUid() +
"/" + postId + "/post_image2");
final StorageReference filepath3 = FirebaseStorage.getInstance().getReference()
.child("posts/users/" + FirebaseAuth.getInstance().getCurrentUser().getUid() +
"/" + postId + "/post_image3");
final StorageReference filepath4 = FirebaseStorage.getInstance().getReference()
.child("posts/users/" + FirebaseAuth.getInstance().getCurrentUser().getUid() +
"/" + postId + "/post_image4");
final StorageReference filepath5 = FirebaseStorage.getInstance().getReference()
.child("posts/users/" + FirebaseAuth.getInstance().getCurrentUser().getUid() +
"/" + postId + "/post_image5");
final Uri[] firebaseUri = new Uri[1];
final Uri[] firebaseUri1 = new Uri[1];
final Uri[] firebaseUri2 = new Uri[1];
final Uri[] firebaseUri3 = new Uri[1];
final Uri[] firebaseUri4 = new Uri[1];
final Uri[] firebaseUri5 = new Uri[1];
filepath.putFile(mSelectedUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
firebaseUri[0] = taskSnapshot.getDownloadUrl();
filepath1.putFile(mSelectedUri1).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
firebaseUri1[0] = taskSnapshot.getDownloadUrl();
filepath2.putFile(mSelectedUri2).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
firebaseUri2[0] = taskSnapshot.getDownloadUrl();
if (mSelectedUri3 != null){ filepath3.putFile(mSelectedUri3).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
firebaseUri3[0] = taskSnapshot.getDownloadUrl();
filepath4.putFile(mSelectedUri4).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
firebaseUri4[0] = taskSnapshot.getDownloadUrl();
filepath5.putFile(mSelectedUri5).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
firebaseUri5[0] = taskSnapshot.getDownloadUrl();
Post post = new Post();
post.setCity(mCity.getText().toString());
post.setContact_email(mContactEmail.getText().toString());
post.setCountry(mCountry.getText().toString());
post.setDescription(mDescription.getText().toString());
post.setPost_id(postId);
post.setPrice(mPrice.getText().toString());
post.setState_province(mStateProvince.getText().toString());
post.setTitle(mTitle.getText().toString());
post.setUser_id(FirebaseAuth.getInstance().getCurrentUser().getUid());
post.setImage(firebaseUri[0].toString());
post.setImage1(firebaseUri1[0].toString());
post.setImage2(firebaseUri2[0].toString());
post.setImage3(firebaseUri3[0].toString());
post.setImage4(firebaseUri4[0].toString());
post.setImage5(firebaseUri5[0].toString());
reference.child(getString(R.string.node_posts))
.child(postId)
.setValue(post);
showProgressBar();
resetFields();
}
});
}//file4
});//file4
} //file3
}); }//file3
}//file2
}); //file2
}//file1
});//file1
}
});
}
I've been struggling for days to figure out a solution for this.
I am using BSImagePicker for multi image selection along side of a form.
So the user chooses images from storage, fill out a form then submit. I limited the number of images to 6.
Everything works fine when all 6 images were selected, but if the user decides to upload less than 6 the app crashes.
Full class: https://pastebin.com/F8y7esud
filepath.putFile(mSelectedUri).addOnSuccessListener
filepath1.putFile(mSelectedUri1).addOnSuccessListener
filepath2.putFile(mSelectedUri2).addOnSuccessListener
filepath3.putFile(mSelectedUri3).addOnSuccessListener
filepath4.putFile(mSelectedUri4).addOnSuccessListener
filepath5.putFile(mSelectedUri5).addOnSuccessListener
If a user selects 2 images then Logcat points to filepath2
If they selects 3 Logcat points to path3 (uri cannot be null)
How do I check for NULL in this situation?
The thing is if I check it like this:
If (mSelectedUri3 != null ) { the rest of the code }
and the user uploads/selects only 3 images then mSelectedUri3 is null and it won't execute it. Just stops there.
if (mAllUri != null ) {
Toast.makeText(getActivity(), "uploading image", Toast.LENGTH_SHORT).show();
final String postId = FirebaseDatabase.getInstance().getReference().push().getKey();
StorageReference filepath = FirebaseStorage.getInstance().getReference()
.child("posts/users/" + FirebaseAuth.getInstance().getCurrentUser().getUid() +
"/" + postId + "/post_image");
final StorageReference filepath1 = FirebaseStorage.getInstance().getReference()
.child("posts/users/" + FirebaseAuth.getInstance().getCurrentUser().getUid() +
"/" + postId + "/post_image1");
final StorageReference filepath2 = FirebaseStorage.getInstance().getReference()
.child("posts/users/" + FirebaseAuth.getInstance().getCurrentUser().getUid() +
"/" + postId + "/post_image2");
final StorageReference filepath3 = FirebaseStorage.getInstance().getReference()
.child("posts/users/" + FirebaseAuth.getInstance().getCurrentUser().getUid() +
"/" + postId + "/post_image3");
final StorageReference filepath4 = FirebaseStorage.getInstance().getReference()
.child("posts/users/" + FirebaseAuth.getInstance().getCurrentUser().getUid() +
"/" + postId + "/post_image4");
final StorageReference filepath5 = FirebaseStorage.getInstance().getReference()
.child("posts/users/" + FirebaseAuth.getInstance().getCurrentUser().getUid() +
"/" + postId + "/post_image5");
final Uri[] firebaseUri = new Uri[1];
final Uri[] firebaseUri1 = new Uri[1];
final Uri[] firebaseUri2 = new Uri[1];
final Uri[] firebaseUri3 = new Uri[1];
final Uri[] firebaseUri4 = new Uri[1];
final Uri[] firebaseUri5 = new Uri[1];
filepath.putFile(mSelectedUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
firebaseUri[0] = taskSnapshot.getDownloadUrl();
filepath1.putFile(mSelectedUri1).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
firebaseUri1[0] = taskSnapshot.getDownloadUrl();
filepath2.putFile(mSelectedUri2).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
firebaseUri2[0] = taskSnapshot.getDownloadUrl();
if (mSelectedUri3 != null){ filepath3.putFile(mSelectedUri3).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
firebaseUri3[0] = taskSnapshot.getDownloadUrl();
filepath4.putFile(mSelectedUri4).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
firebaseUri4[0] = taskSnapshot.getDownloadUrl();
filepath5.putFile(mSelectedUri5).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
firebaseUri5[0] = taskSnapshot.getDownloadUrl();
Post post = new Post();
post.setCity(mCity.getText().toString());
post.setContact_email(mContactEmail.getText().toString());
post.setCountry(mCountry.getText().toString());
post.setDescription(mDescription.getText().toString());
post.setPost_id(postId);
post.setPrice(mPrice.getText().toString());
post.setState_province(mStateProvince.getText().toString());
post.setTitle(mTitle.getText().toString());
post.setUser_id(FirebaseAuth.getInstance().getCurrentUser().getUid());
post.setImage(firebaseUri[0].toString());
post.setImage1(firebaseUri1[0].toString());
post.setImage2(firebaseUri2[0].toString());
post.setImage3(firebaseUri3[0].toString());
post.setImage4(firebaseUri4[0].toString());
post.setImage5(firebaseUri5[0].toString());
reference.child(getString(R.string.node_posts))
.child(postId)
.setValue(post);
showProgressBar();
resetFields();
}
});
}//file4
});//file4
} //file3
}); }//file3
}//file2
}); //file2
}//file1
});//file1
}
});
}