Veena Veena - 4 months ago 12
Android Question

copy from local Arraylist to global ArrayList in android

I have a method like this it just splits the image into few chunks

public void splitImage(Bitmap bmp, int chunks, int imageCode) {

int chunkHeight, chunkWidth;
int rows, cols;
rows = cols = 5;

//To store all the small image chunks in bitmap format in this list
ArrayList<Bitmap> greyImageChunks = new ArrayList<Bitmap>(chunks);
ArrayList<Bitmap> greenImageChunks = new ArrayList<Bitmap>(chunks);
ArrayList<Bitmap> redImageChunks = new ArrayList<Bitmap>(chunks);


//Getting the scaled bitmap of the source image
//BitmapDrawable drawable = (BitmapDrawable) image.getDrawable();
//Bitmap bitmap = drawable.getBitmap();
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bmp, bmp.getWidth(), bmp.getHeight(), true);

rows = cols = (int) Math.sqrt(chunks);
chunkHeight = bmp.getHeight()/rows;
chunkWidth = bmp.getWidth()/cols;

//xCoord and yCoord are the pixel positions of the image chunks
int yCoord = 0;
for(int x=0; x<rows; x++){
int xCoord = 0;
for(int y=0; y<cols; y++){

if(imageCode == 1) {
greyImageChunks.add(Bitmap.createBitmap(scaledBitmap, xCoord, yCoord, chunkWidth, chunkHeight));
} else if(imageCode == 2) {
greenImageChunks.add(Bitmap.createBitmap(scaledBitmap, xCoord, yCoord, chunkWidth, chunkHeight));
} else if(imageCode == 3) {
redImageChunks.add(Bitmap.createBitmap(scaledBitmap, xCoord, yCoord, chunkWidth, chunkHeight));
}

xCoord += chunkWidth;
}
yCoord += chunkHeight;
}
}


Here I would like to retrieve chunks from the greyImageChunks, greenImageChunks and redImageChunks using another method say mergeImages().

These are not accessed by outside world because they are locally created.
Is there any method to access these array Lists outside of this method.
Can any copy method from one local ArrayList to another global ArrayList possible?
Please help me.

Note: ArrayList size should be dynamic because the size of ArrayList is defined using int chunks in splitImage() method

Answer

If i understood correctly, Create one Global ArrayList say mArrayList.

ArrayList<Bitmap> mArrayList= new ArrayList<Bitmap>();

Now inside your method, after for loop add all three local ArrayLists to Global ArrayList.

for(int x=0; x<rows; x++){
        int xCoord = 0;
        for(int y=0; y<cols; y++){

            if(imageCode == 1) {
                greyImageChunks.add(Bitmap.createBitmap(scaledBitmap, xCoord, yCoord, chunkWidth, chunkHeight));    
            } else if(imageCode == 2) {
                greenImageChunks.add(Bitmap.createBitmap(scaledBitmap, xCoord, yCoord, chunkWidth, chunkHeight));
            } else if(imageCode == 3) {    
                redImageChunks.add(Bitmap.createBitmap(scaledBitmap, xCoord, yCoord, chunkWidth, chunkHeight));
            }

            xCoord += chunkWidth;
        }
        yCoord += chunkHeight;
    }
mArrayList.addAll(greyImageChunks);
mArrayList.addAll(greenImageChunks);
mArrayList.addAll(redImageChunks);
Comments