0

so I'm going to break this down as clearly as I can.

1) So I queried an array of ParseFile Images from Parse and I want to convert them into a Bitmap array. So here is the code for that.

public void queryImage() {
    ParseQuery<ParseObject> query = ParseQuery.getQuery("TestObject");

    //So lets queryImage all the images that belong to the user.
    query.setLimit(2);
    query.whereExists("ImageFile");
    query.orderByDescending("createdAt");
    try {
        List<ParseObject> lists2 = query.find();
        for(ParseObject cardImage : lists2) {
            ParseImageFileArrayList.add((ParseFile) cardImage.get("ImageFile"));
        }
        convertFileArray(ParseImageFileArrayList);//Here is another I created
    } catch(ParseException e) {
        e.printStackTrace();
    }
}

2) Now in that method I called convertFileArray() method, which takes in that parsefile array and converts it into a bitmap. Here is the code for that.

private void convertFileArray(ArrayList<ParseFile> arrayList) {
    for(ParseFile file: arrayList) {
        if(file != null) {
            file.getDataInBackground(new GetDataCallback() {
                @Override
                public void done(byte[] bytes, ParseException e) {
                    if(e == null) {
                        bmp1 = BitmapFactory.decodeByteArray(bytes,0,bytes.length);
                        BitmapArray.add(bmp1);
                    } else {

                    }
                }
            });
        }
    }
}

3) Okay, now I want to loop through a string of Names, "John, Mary, Adam, Susan, etc.) And for each name at that position I want to add the corresponding Image from the Bitmap array. So if John is at position 0 in the name Array, I should add the image at Position 0 of John from my Byte array into a class I call "CardModel" Here is the code. And mind you, these 3 methods are all in 1 class.

public void queryDoneSetCards() {//Here we need to make an Array of Cardmodels.
        for(int i = 0; i < queryCardNames.size(); i++) {
            CardModel cardModel = new CardModel();
            cardModel.setImageAsBitmap(BitmapArray.get(i));//HERE IS WHERE IM GETTING THE ERROR!
            CardModelArray.add(cardModel);
        }
}

4) Finally when I get to another activity, where the user clicks "Done" (the queryImage method gets called first, which has the convertFileArray() method in it that gets called) then the queryDoneSetCards() method gets called. And I get this an index out of bounds exception I cant figure out why its not working.

Process: com.lorentzos.swipecards.example, PID: 23182
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0


12-08 13:26:47.218 23182-23182/com.lorentzos.swipecards.example E/AndroidRuntime: FATAL EXCEPTION: main
             Process: com.lorentzos.swipecards.example, PID: 23182
             java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
             at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
             at java.util.ArrayList.get(ArrayList.java:308)
             at com.lorentzos.swipecards.MyParse.MyParse.queryDoneSetCards(MyParse.java:285)
             at com.lorentzos.swipecards.MyParse.MyParse.convertFileArray(MyParse.java:267)
             at com.lorentzos.swipecards.MyParse.MyParse.queryImage(MyParse.java:143)
             at com.lorentzos.swipecards.CardFinal.Card_FINALE.onClick(Card_FINALE.java:258)
             at android.view.View.performClick(View.java:4803)
             at android.view.View$PerformClick.run(View.java:19981)
             at android.os.Handler.handleCallback(Handler.java:739)
             at android.os.Handler.dispatchMessage(Handler.java:95)
             at android.os.Looper.loop(Looper.java:135)
             at android.app.ActivityThread.main(ActivityThread.java:5430)
             at java.lang.reflect.Method.invoke(Native Method)
             at java.lang.reflect.Method.invoke(Method.java:372)
             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:913)
             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:706)
3
  • Please add you full stacktrace with the out of bounds error in it. Commented Dec 8, 2015 at 21:55
  • Your error is on the for loop of the queryDoneSetCards() method. Try setting some breakpoints or placing some log statements there to ensure that the queryCardNames collection has data and exists Commented Dec 8, 2015 at 22:07
  • 1
    @doubleA The querycardNames is fine. It gives me an error on cardModel.setImageAsBitmap(BitmapArray.get(i)); Showing that my BitmapArray is empty...Maybe because I'm doing it in the background thread. Is there an alternative to file.getDataInBackground in the convertFileArray() method? Commented Dec 8, 2015 at 22:14

2 Answers 2

1

The size of queryCardNames is not the same as BitmapArray, this is why you get the IndexOutOfBoundsException.

In queryDoneSetCards() method, change

for(int i = 0; i < queryCardNames.size(); i++) 

to

for(int i = 0; i < BitmapArray.size(); i++)
Sign up to request clarification or add additional context in comments.

4 Comments

I tried that, and BitmapArray was giving me an indexOutOfBounds exeception as well. It was saying the size was 0..
If BitmapArray size is 0, it won't enter the loop, how you get IndexOutOfBoundsException??
Well, when i used queryCardNames array for the for loop and had the cardModel.setImageAsBitmap(BitmapArray.get(i)); in the for loop I got the indexoutofBounds exception. But if I do what you posted, I still get the index out of bounds exception. Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
If you are getting data in background and adding the data to the array in the callback then depending on where you call query card names you will not have any data there yet.
1

While working with parse I found that it was useful to put some of my calls into an async task. Specifically the parts where some data relied on other data being fetched. It was useful to just get all the data in an async task and then use it rather than trying to chain async parse calls.

private class MyAsync extends AsyncTask<> {

     protected void doInBackground() {
         queryImage()
     }

     protected void onPostExecute(Long result) {
         queryDoneSetCards();
     }
}

public void queryImage() {
    ParseQuery<ParseObject> query = ParseQuery.getQuery("TestObject");

    //So lets queryImage all the images that belong to the user.
    query.setLimit(2);
    query.whereExists("ImageFile");
    query.orderByDescending("createdAt");
    try {
        List<ParseObject> lists2 = query.find();
        for(ParseObject cardImage : lists2) {
            ParseImageFileArrayList.add((ParseFile) cardImage.get("ImageFile"));
        }
        convertFileArray(ParseImageFileArrayList);//Here is another I created
    } catch(ParseException e) {
        e.printStackTrace();
    }
}

Your convert file array would be synchronous.

private void convertFileArray(ArrayList<ParseFile> arrayList) {
    for(ParseFile file: arrayList) {
        if(file != null) {
            byte[] bytes = file.getData();
            bmp1 = BitmapFactory.decodeByteArray(bytes,0,bytes.length);
            BitmapArray.add(bmp1);
        }
    }
}

Then call queryDoneSetCards in the onPostExecute() of the async task. You should also probably have a check to make sure that your BitmapArray and name Array have the same size before executing the queryDoneSetCards() loop

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.