jeudi 3 septembre 2015

Error on saving image in sqlite in android

I am trying to download image and save it to db i download it and save i to BLOB field when want to retrieve it it get me null value.I search a lot and examine lots of things but I have no idea where is the problem :( .

save image part :

MyDataBase = new MyDatabase(this);
mydb = MyDataBase.getWritableDatabase();

byte[] tempval;
for (int j = 0; j < myarray.arrtitle.length; j++)
{
  tempval = saveimage(myarray.arrpic.get(j),myarray.arrpath[j]);
  mydb.execSQL("INSERT INTO news (title,content,image) VALUES  (\"" + myarray.arrtitle[j] +
  "\",\"" + myarray.arrcontent[j] + "\",\"" + tempval  + "\")");
}
mydb.close() 

saveimage() function :

try {
            ////////////////get bitmap to byte
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
            byte[] image = bos.toByteArray();

            return image;

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }

read from db:

          try 
            {
                String temp = null;
                Bitmap tempbit = null;
                ArrayList<Bitmap> tempbit2 = new ArrayList<Bitmap>();
                ArrayList<String> tempstring = new ArrayList<String>();

                MyDataBase = new MyDatabase(this);
                mydb = MyDataBase.getReadableDatabase();
                Cursor cu = mydb.rawQuery("SELECT * FROM news",null);
                cu.moveToFirst();
                for (int i = 0;i<cu.getCount();i++)
                {

                    temp = cu.getString(cu.getColumnIndex("title"));
                    byte[] imag = cu.getBlob(cu.getColumnIndex("image"));
                    tempbit = BitmapFactory.decodeByteArray(imag, 0, imag.length);
                    tempbit2.add(tempbit);
                    tempstring.add(temp);
                    cu.moveToNext();
                    Toast.makeText(getApplicationContext(), "" + tempbit + i, Toast.LENGTH_LONG).show();

                }
                ////////////////////////////show in list view
                ListViewAdapte adapter = new ListViewAdapte(Description.this, R.layout.listview, tempstring , tempbit2); 
                MyList.setAdapter(adapter);
                MyList.setOnItemClickListener(this);

            } catch (Exception e) {
                // TODO: handle exception
                Toast.makeText(getApplicationContext(), "error on load from db", Toast.LENGTH_LONG).show();
            }

When I Toast tempbit it gets me null

Aucun commentaire:

Enregistrer un commentaire