在db erro&中保存图像decodeByteArray给出null

时间:2015-09-03 10:20:26

标签: android sqlite save-image

我正在尝试下载图像并将其保存到数据库我下载它并将我保存到BLOB字段时想要检索它它得到我的null值。我搜索了很多并检查了很多东西,但我不知道在哪里问题:(。

保存图片部分:

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()函数:

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;
    }

从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();
            }

当我Toast tempbit时,它会让我无效

1 个答案:

答案 0 :(得分:1)

我应该将图片保存为内容视图

       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);

                    }

使用contentview可以保存图像:)