1

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); // i get null here
                    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

  • You're displaying an error using `Toast`. Put `e.printStackTrace()` in `catch` clause and let us know the exception in logcat. – Glenn Sep 03 '15 at 10:23
  • You're probably suffering `OutOfMemory` error. – Glenn Sep 03 '15 at 10:24
  • @Glenn-- i dont have any error in my app. i can show u my data base its full of data. what do u mean by **OutOfMemory** –  Sep 03 '15 at 10:39
  • I thought you have an error saving image? – Glenn Sep 03 '15 at 10:45
  • In `saveImage()` function. Check if you're getting an exception/error there. You're creating another bitmap as byte[] which occupies more space in memory. – Glenn Sep 03 '15 at 10:46
  • I think all things are ok. :\ this is my db picture : http://uupload.ir/files/fu3a_db.png –  Sep 03 '15 at 11:00
  • when i am reading from the db i can load titles but not images –  Sep 03 '15 at 11:01
  • Why images as text from database less than 10 characters? It is not actually complate image binary. Open an image using text editor and you will see a complete image's binary as text. – Glenn Sep 03 '15 at 11:09
  • @Glenn-- I think its a bitmap time that store in my database. i dont know why because i do all things good i think ;( –  Sep 05 '15 at 14:55

1 Answers1

1

i should save picture as contentview

       ContentValues tempval = new ContentValues();
                    for (int j = 0; j < myarray.arrtitle.length; j++)
                    {
                        mydb.execSQL("INSERT INTO news (title,content) VALUES  (\"" + myarray.arrtitle[j] +
                        "\",\"" + myarray.arrcontent[j] + "\"" + ")");
                     }



                    for (int j = 0; j < myarray.arrpic.size(); j++)
                    {

                        tempval = saveimage(myarray.arrpic.get(j),myarray.arrpath[j]);

                        update ="title ='" + myarray.arrtitle[j] + "'" ;

                         mydb.update("news", tempval,update, null);

                    }

save image is possible with using contentview :)