Ad

How To Upload Multiple Images To Firebase In Android?

I know this question has been asked here and answered but for some reasons, provided solutions are not working for me.

So here is how I'm trying to do this. I have two buttons in my layout one to open gallery and other to upload the images.

Defined members

int SELECT_PICTURES = 1;

ArrayList<Uri> mArrayUri = new ArrayList<Uri>();
Uri imageUri;
int up = 0;
int k =0;

Gallery button code

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("image/*");
    intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURES);

On Activity result code

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == SELECT_PICTURES) {
        if (resultCode == MainActivity.RESULT_OK) {
            if (data.getClipData() != null) {
                int count = data.getClipData().getItemCount();
                Log.i("count", String.valueOf(count));
                int currentItem = 0;
                while (currentItem < count) {
                    imageUri = data.getClipData().getItemAt(currentItem).getUri();


                    Log.i("uri", imageUri.toString());
                    mArrayUri.add(imageUri);
                    currentItem = currentItem + 1;
                }
                Log.i("listsize", String.valueOf(mArrayUri.size()));
            } else if (data.getData() != null) {
                String imagePath = data.getData().getPath();

            }
        }
    }
}

Upload button code

  StorageReference filepath = FirebaseStorage.getInstance().getReference().child("gpic");


    while (up < mArrayUri.size()){

        // error is pointing to this line, line 82
        filepath.child(mArrayUri.get(k).getLastPathSegment()).putFile(mArrayUri.get(k)).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                Uri downloadURL = taskSnapshot.getDownloadUrl();
                Toast.makeText(TestingActivity.this, downloadURL.toString(), Toast.LENGTH_SHORT).show();
                up++;
                k++;
            }
        });
    }
}

The above code uploads one image and then crashes with the following error

01-24 12:37:37.416 8336-8336/com.codenemesis.uploading E/AndroidRuntime: FATAL EXCEPTION: main
                                                                   Process: com.codenemesis.uploading, PID: 8336
                                                                   java.lang.IllegalStateException: Could not execute method for android:onClick
                                                                       at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                                                                       at android.view.View.performClick(View.java:5610)
                                                                       at android.view.View$PerformClick.run(View.java:22265)
                                                                       at android.os.Handler.handleCallback(Handler.java:751)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                       at android.os.Looper.loop(Looper.java:154)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
                                                                    Caused by: java.lang.reflect.InvocationTargetException
                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                       at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                                                                       at android.view.View.performClick(View.java:5610) 
                                                                       at android.view.View$PerformClick.run(View.java:22265) 
                                                                       at android.os.Handler.handleCallback(Handler.java:751) 
                                                                       at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                       at android.os.Looper.loop(Looper.java:154) 
                                                                       at android.app.ActivityThread.main(ActivityThread.java:6077) 
                                                                       at java.lang.reflect.Method.invoke(Native Method) 
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 
                                                                    Caused by: java.util.concurrent.RejectedExecutionException: Task [email protected] rejected from [email protected][Running, pool size = 2, active threads = 2, queued tasks = 128, completed tasks = 0]
                                                                       at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2049)
                                                                       at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:814)
                                                                       at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1360)
                                                                       at com.google.firebase.storage.zzu.zzt(Unknown Source)
                                                                       at com.google.firebase.storage.UploadTask.schedule(Unknown Source)
                                                                       at com.google.firebase.storage.StorageTask.zzcls(Unknown Source)
                                                                       at com.google.firebase.storage.StorageReference.putFile(Unknown Source)
                                                                       at **com.codenemesis.uploading.TestingActivity.up(TestingActivity.java:82)**
                                                                       at java.lang.reflect.Method.invoke(Native Method) 
                                                                       at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                                                                       at android.view.View.performClick(View.java:5610) 
                                                                       at android.view.View$PerformClick.run(View.java:22265) 
                                                                       at android.os.Handler.handleCallback(Handler.java:751) 
                                                                       at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                       at android.os.Looper.loop(Looper.java:154) 
                                                                       at android.app.ActivityThread.main(ActivityThread.java:6077) 
                                                                       at java.lang.reflect.Method.invoke(Native Method) 
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 
Ad

Answer

Found what was causing the problem. I was incrementing the variables "up" and "k" within the onSuccessListener. That might be changing their values before uploading the URI, so I incremented them outside the onSuccessListener and it worked perfectly fine.

Upload button code

StorageReference filepath = FirebaseStorage.getInstance().getReference().child("gpic");

    while (up < mArrayUri.size()){

        filepath.child(mArrayUri.get(k).getLastPathSegment()).putFile(mArrayUri.get(k)).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                Uri downloadURL = taskSnapshot.getDownloadUrl();
                Toast.makeText(TestingActivity.this, downloadURL.toString(), Toast.LENGTH_SHORT).show();

            }
        });
        up++;
        k++;

    }

}
Ad
source: stackoverflow.com
Ad