dimanche 19 avril 2015

Loading images from SQlite to gridview

Whenever I load my images from Sqlite the Activity doesn't even start,my device just stops responding and even after waiting for some time it nothing is visible on screen.


Second question is whenever I remove bm.recycle() or b.recycle() from the code, it gives an error saying OutOfMemory exception. So should I keep them or not? If not what should i do to remove the OutOfMemory Exception?


Here is my code:



public class ApplyLeave extends Activity {
ArrayList<String> name=new ArrayList<>();
ArrayList<String> description=new ArrayList<>();
ArrayList<Float> price=new ArrayList<>();
ArrayList<Bitmap> front=new ArrayList<>();
GridView g;
byte[] f;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar bar=getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#FF989898")));
setContentView(R.layout.activity_apply_leave);
DbHelper help=new DbHelper(this);
Cursor cr=help.displayProducts();
cr.moveToFirst();
do{
name.add(cr.getString(cr.getColumnIndex("Name")));
description.add(cr.getString(cr.getColumnIndex("Description")));
price.add(cr.getFloat(cr.getColumnIndex("Price")));
if(cr.getBlob(cr.getColumnIndex("FrontSide"))!=null) {
f = cr.getBlob(cr.getColumnIndex("FrontSide"));
}
else if(cr.getBlob(cr.getColumnIndex("LeftSide"))!=null)
{
f = cr.getBlob(cr.getColumnIndex("LeftSide"));
}
else if(cr.getBlob(cr.getColumnIndex("RightSide"))!=null)
{
f = cr.getBlob(cr.getColumnIndex("RightSide"));
}
BitmapFactory.Options options=new BitmapFactory.Options();// Create object of bitmapfactory's option method for further option use
options.inPurgeable = true; // inPurgeable is used to free up memory while required
options.inSampleSize=2;
Bitmap bm=BitmapFactory.decodeByteArray(f,0,f.length,options);
Bitmap b=Bitmap.createScaledBitmap(bm,50,50,true);
ByteArrayOutputStream out = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG, 100, out);
front.add(b);
bm.recycle();
b.recycle();
}while(cr.moveToFirst());
g = (GridView)findViewById(R.id.g);
g.setAdapter(new ImageAdapter(this));

}


And my ImageAdapter Class



public class ImageAdapter extends BaseAdapter{
Context c;
public ImageAdapter(Context context) {
c=context;
}

@Override
public int getCount() {
DbHelper help=new DbHelper(getApplication());
Cursor cr=help.displayProducts();

return cr.getCount();
}

@Override
public Object getItem(int position) {
return null;
}

@Override
public long getItemId(int position) {
return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view=convertView;
if ( convertView == null )
{
LayoutInflater inflater=getLayoutInflater();
view=inflater.inflate(R.layout.gridview,null);
TextView n= (TextView)findViewById(R.id.Name);
TextView p=(TextView)findViewById(R.id.Price);
p.setText(price.get(position).toString());
n.setText(name.get(position));
ImageView image=(ImageView)findViewById(R.id.RightSide);
image.setScaleType(ImageView.ScaleType.CENTER_CROP);
image.setImageBitmap(front.get(position));
}
return view;
}

Aucun commentaire:

Enregistrer un commentaire