I have made a simple android application that displays images in a gridview and you can select some images and send them. This is my getView method.
public View getView(int position, View convertView, ViewGroup parent) { View v; if (convertView == null) { LayoutInflater li = getLayoutInflater(); v = li.inflate(R.layout.imageview, null);
final CheckBox cb = (CheckBox) v.findViewById(R.id.checkbox);
cb.setOnCheckedChangeListener(new AddPhotos());
ImageView iv = (ImageView) v.findViewById(R.id.image);
//make image clickable so that it is checked when clicked on the image
iv.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
cb.setChecked((cb.isChecked() ? false : true));
}
});
imagecursor.moveToPosition(position);
uriList.add(imagecursor.getString(image_column_index));
File image = new File(imagecursor.getString(image_column_index));
Bitmap actualBitmap = resizeBitMapImage(image.getPath(), image_width, image_height);
iv.setImageBitmap(actualBitmap);
iv.setLayoutParams(new FrameLayout.LayoutParams(95, 95));
System.gc();
}
else
{
v = convertView;
System.gc();
}
return v;
}
}
Now the problem wit this was that for less images i.e when all images are displayed without any need to scroll, it was working fine. But if there were images not visible and you have to scroll down, then it showed a strange behavior by repeating the earlier images and changing the checked state of the images when you scroll down or up. I changed the getView method like
public View getView(int position, View convertView, ViewGroup parent)
{
View v;
if (convertView == null)
{
LayoutInflater li = getLayoutInflater();
v = li.inflate(R.layout.imageview, null);
}
else
{
v = convertView;
System.gc();
}
final CheckBox cb = (CheckBox) v.findViewById(R.id.checkbox);
cb.setOnCheckedChangeListener(new AddPhotos());
ImageView iv = (ImageView) v.findViewById(R.id.image);
// make image clickable so that it is checked when clicked on the image
iv.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
cb.setChecked((cb.isChecked() ? false : true));
}
});
imagecursor.moveToPosition(position);
uriList.add(imagecursor.getString(image_column_index));
File image = new File(imagecursor.getString(image_column_index));
Bitmap actualBitmap = resizeBitMapImage(image.getPath(), image_width, image_height);
iv.setImageBitmap(actualBitmap);
iv.setLayoutParams(new FrameLayout.LayoutParams(95, 95));
System.gc();
return v;
}
}
Now the order of the images is not changing but scrolling up or down throws an ANR and the checked state is also changed (i guess randomly). the traces.txt shows that it is coming from bitMapImage = BitmapFactory.decodeFile(filePath, options); line in the resizeBitMapImage method which is as below
public static Bitmap resizeBitMapImage(String filePath, int targetWidth, int targetHeight)
{
Bitmap bitMapImage = null;
// First, get the dimensions of the image
Options options = new Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
double sampleSize = 0;
// Only scale if we need to
// (16384 buffer for img processing)
Boolean scaleByHeight = Math.abs(options.outHeight - targetHeight) >= Math.abs(options.outWidth - targetWidth);
if (options.outHeight * options.outWidth * 2 >= 1638)
{
// Load, scaling to smallest power of 2 that'll get it <= desired
// dimensions
sampleSize = scaleByHeight ? options.outHeight / targetHeight : options.outWidth / targetWidth;
sampleSize = (int) Math.pow(2d, Math.floor(Math.log(sampleSize) / Math.log(2d)));
}
// Do the actual decoding
options.inJustDecodeBounds = false;
options.inTempStorage = new byte[128];
while (true)
{
try
{
options.inSampleSize = (int) sampleSize;
bitMapImage = BitmapFactory.decodeFile(filePath, options);
break;
}
catch (Exception ex)
{
try
{
sampleSize = sampleSize * 2;
}
catch (Exception ex1)
{
}
}
}
return bitMapImage;
}
Now i am very new to android development and am very confused. Please any help will be greatly appreciated
EDIT: The images are being added to gridview and the getView method is for the imageAdapter for this gridview.