mercredi 4 février 2015

Android- Insert blob into a SQLite database column [duplicate]


This question already has an answer here:




I converted array of bitmap images into Blob.I need to insert it into a sqlite database.and set the values into a list adapter.But it thows a Nullpointer exception in line : Bitmap b1=BitmapFactory.decodeByteArray(abc, 0, abc.length); and crashes my device.


MainActivity.java



public class MainActivity extends Activity{

ListView prd_list;
public static Integer clas;
public static byte [] img,img1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prd_list = (ListView) findViewById(R.id.list);
DataBaseHandler dbh = new DataBaseHandler(this);

/////////////////convert png images to bitmap//////////

Bitmap[] images = { BitmapFactory.decodeResource(getResources
(),R.drawable.candle1),BitmapFactory.decodeResource(getResources
(),R.drawable.candl3),BitmapFactory.decodeResource(getResources(),R.drawable.candl4),BitmapFactory.decodeResource(getResources(),R.drawable.candl5)};
ByteArrayOutputStream bos=new ByteArrayOutputStream();
// images.compress(Bitmap.CompressFormat.PNG, 100, bos);
img=bos.toByteArray();



////////////////////////////////////////////////////////


SQLiteDatabase db = dbh.getWritableDatabase();
Cursor cr = db.rawQuery("SELECT * FROM product", null);
final String[] pname = new String[cr.getCount()];
String[] price = new String[cr.getCount()];

String[] pmge=new String[cr.getCount()];

int i = 0;
while(cr.moveToNext())
{
String name = cr.getString(cr.getColumnIndex("pname"));
String prprice = cr.getString(cr.getColumnIndex("pprice"));
img=cr.getBlob(cr.getColumnIndex("pimage"));

pname[i] = name;
price[i] = prprice;
i++;
}

ListAdapter adapter = new ListAdapter(this, pname, price);
prd_list.setAdapter(adapter);

}
private ListAdapter getListAdapter() {
// TODO Auto-generated method stub
return null;
}

}


DataBaseHandler.java



public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub

db.execSQL("CREATE TABLE IF NOT EXISTS product(pimage BLOB,pid INTEGER PRIMARY KEY,pname TEXT,pprice NUMERIC,pspec TEXT,pfeature TEXT)");

db.execSQL("INSERT INTO product(pimage,pname,pprice,pspec) VALUES('img','Candle stick 1',4000,'Solar garden / pathway light,Solar Panel:1pc crystal silicon solar cell, Battery:1pc 1.2V Ni-MH/Ni-CD AA battery 600MA ,Material:Stainless steel ,WaterProof and safe ')");


Listadapter.java



public class ListAdapter extends BaseAdapter {

byte[] abc=MainActivity.img;

private final String[] pname;
private final String[] price;

private Context mcontext;
public ListAdapter(Context c,String[] pname,String[] price){

mcontext=c;
this.pname=pname;
this.price=price;

}

@Override
public int getCount() {
// TODO Auto-generated method stub
return pname.length;
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View List;
LayoutInflater mLayoutinflater=(LayoutInflater) mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(convertView==null){
List=new View(mcontext);

List=mLayoutinflater.inflate(R.layout.mylist, parent, false);
}
else{
List = (View)convertView;
}

TextView textView1 = (TextView)List.findViewById(R.id.pr_name);
TextView textView2 = (TextView)List.findViewById(R.id.pr_price);
ImageView imageview= (ImageView)List.findViewById(R.id.pr_img);
textView1.setText(pname[position].toString());
textView2.setText("Rs "+price[position] +" /-");

////////////error line/////////////

Bitmap b1=BitmapFactory.decodeByteArray(abc, 0, abc.length);

///////////////////////////////////////////
imageview.setImageBitmap(b1);


// imageview.setImageResource(pmge[position]);
// TODO Auto-generated method stub
return List;

}

}

Aucun commentaire:

Enregistrer un commentaire