0

I have several string arrays stored in my database that contain file names. I want to loop through this array to return the files one by one that are stored in the internal storage. Example of one of the array:

[["12","21","31"],["empty","22","32"],["13","23","33"]]// this is the array unmodified

Below is the code I have now but just gives me an index error as the index is 12 at the start because of the array begins at 12.

layout = layout.replaceAll("\"empty\",?", "").replaceAll("[\"\\]\\ 
 [\"]+","").replaceAll("^\"|\"$", "");  //this removes the "empty" string
    String[] layoutArray = layout.split(",");


    int rows = 3;
    int columns = 3;

    int layoutElement = 0;
    try {
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                // get the image from the internal storage
                int imageIndex = Integer.valueOf(layoutArray[layoutElement]) - 1;
                String imageFile = layoutArray[imageIndex];
                Bitmap image = BitmapFactory.decodeFile(new File(getFilesDir(), imageFile).getAbsoluteFile().toString());
                mImageList.add(new Grid(getApplicationContext(), i, j, image, imageFile));
                layoutElement++;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

I know the code I have is completely wrong in logic but I need help with this and can't get my head around it. each of the array values has a file name stored by that number, I removed "empty" because it is not needed. My end goal is to place these files(that are images) into a grid view.

2
  • paste error log Commented Mar 20, 2018 at 1:04
  • @Mohammad java.lang.ArrayIndexOutOfBoundsException: length=1; index=12 Commented Mar 20, 2018 at 1:09

1 Answer 1

1
  • You are splitting the text using "," which splits the array - that you provided as example - into 9 element ... you need to replace all "],[" with something like "]-[" and split the string using "-" .

    layout = layout.replaceAll("\\] , \\[", "\\] - \\[");  
    String[] layoutArray = layout.split("-");
    
  • You are incrementing the value of layoutElement for each nested loop without resetting it in the first loop >> this code should work as expected

    layout = layout.replaceAll("\"empty\",?", "").replaceAll("^\"|\"$", "").replaceAll("\\],\\[", "\\]-\\[");
        String[] layoutArray = layout.split("-");
    
    try {
        for (int i = 0; i < layoutArray.length; i++) {
            layoutArray[i]= layoutArray[i].replaceAll("[\\[\"\\]]","");
            String[] splitted = layoutArray[i].split(",");
            for (int j = 0; j < splitted.length; j++) {
                int imageIndex = Integer.valueOf(splitted[j]) - 1;
                String imageFile = splitted[imageIndex];
                Bitmap image = BitmapFactory.decodeFile(new File(getFilesDir(), imageFile).getAbsoluteFile().toString());
                mImageList.add(new Grid(getApplicationContext(), i, j, image, imageFile));
    
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    
Sign up to request clarification or add additional context in comments.

10 Comments

Thank for the reply! I tried your solution but now I get the error java.lang.ArrayIndexOutOfBoundsException: length=8; index=11
I've included another problem in the answer which could cause this issue
there is 8 just for this array after removing the "empty" string with regex. the files im trying to retrieve are all under the name of each element. So "12" has a file in internal storage named "12"
Yes same error. for some reason the index is using the actual value of the element giving length = 1 and index = 12
stop the code that deletes empty from the array and run again
|

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.