• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Uploading multiple images to firebase database

rocks860

Lurker
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
}
});
}
 
Need to see the stack trace.

When I select 3 images
Here is : https://pastebin.com/JWPsGrhm

at codingwithmitch.com.forsale.PostFragment$2$1$1$1.onSuccess(PostFragment.java:198) this points to -->

filepath2.putFile(mSelectedUri2).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {

at codingwithmitch.com.forsale.PostFragment$2$1$1$1.onSuccess(PostFragment.java:204) points to -->

filepath3.putFile(mSelectedUri3).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {

When I select 4 images

https://pastebin.com/q3bgSRCN

Line 210 -->
filepath4.putFile(mSelectedUri4).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {

But if I select all 6 images everything uploads and saves just fine.



 
Your source code is out of sync with the stack trace. It doesn't match up

According to the exception stack trace, line 195 is causing the problem.

Code:
at codingwithmitch.com.forsale.PostFragment$2$1$1$1.onSuccess(PostFragment.java:195)

But line 195 in your posted code is a blank line.
 
Your source code is out of sync with the stack trace. It doesn't match up

According to the exception stack trace, line 195 is causing the problem.

Code:
at codingwithmitch.com.forsale.PostFragment$2$1$1$1.onSuccess(PostFragment.java:195)

But line 195 in your posted code is a blank line.


195 points to
filepath3.putFile(mSelectedUri3).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {

189 poits to
filepath2.putFile(mSelectedUri2).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {

Of course it depends how many images were selected. If only one then it will point to
filepath1.putFile(mSelectedUri1).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {

Because image uri is null

I deleted some comments so lines were off a bit. Try this https://pastebin.com/FFbK2m85
 
Last edited:
Your source code is out of sync with the stack trace. It doesn't match up

According to the exception stack trace, line 195 is causing the problem.

Code:
at codingwithmitch.com.forsale.PostFragment$2$1$1$1.onSuccess(PostFragment.java:195)

But line 195 in your posted code is a blank line.

I couldn't go anywhere where that code, so I tried a loop, but it only uploads the last image!

https://pastebin.com/jn2k5Pi6
 
To be blunt, some of your code's messy. This is unnecessary

Code:
        ivImage1 = view.findViewById(R.id.iv_image1);
        ivImage2 = view.findViewById(R.id.iv_image2);
        ivImage3 = view.findViewById(R.id.iv_image3);
        ivImage4 = view.findViewById(R.id.iv_image4);
        ivImage5 = view.findViewById(R.id.iv_image5);
        ivImage6 = view.findViewById(R.id.iv_image6);

Rather than declare separate variables, using a data structure such as an ArrayList.

Code:
List<ImageView> imageViews = new ArrayList<>();
// Code to initialise the list

then later..

Code:
ImageView iv = imageViews.get(i);
 
To be blunt, some of your code's messy. This is unnecessary

Code:
        ivImage1 = view.findViewById(R.id.iv_image1);
        ivImage2 = view.findViewById(R.id.iv_image2);
        ivImage3 = view.findViewById(R.id.iv_image3);
        ivImage4 = view.findViewById(R.id.iv_image4);
        ivImage5 = view.findViewById(R.id.iv_image5);
        ivImage6 = view.findViewById(R.id.iv_image6);

Rather than declare separate variables, using a data structure such as an ArrayList.

Code:
List<ImageView> imageViews = new ArrayList<>();
// Code to initialise the list

then later..

Code:
ImageView iv = imageViews.get(i);

I am trying to use a loop the way this guy described https://stackoverflow.com/questions...he-storge-in-firebase-and-get-the-uri-for-thi

He said, arrayListImageFile is the ArrayList containing your files. In my case
uriList.get(i) is holding the files, but doing this gives me an error:

Code:
for (int i = 0; i < uriList.size(); i++) {
    Uri uri = Uri.fromFile(uriList.get(i));
    uploadMethod(uri, i);
}

' fromFile (java.io.File) in Uri cannot be applied to (android.net.uri).
How do I do the conversion?

Thank you
 
Last edited by a moderator:
So when you get problems like this, your first call is the documentation. Fortunately Google have provided a comprehensive set of Javadocs for the Android SDK.
Looking at the page for the Uri.fromFile() method

https://developer.android.com/reference/android/net/Uri.html#fromFile(java.io.File)

You can see that the parameter to this method is of type File

Code:
public static Uri fromFile (File file)

And what you're trying to do is call this method with a parameter type of android.net.uri.
 
Back
Top Bottom