1

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

保存图像部分:

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

保存图像()函数:

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

从数据库读取:

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

当我敬酒 tempbit 时,它让我为空

4

1 回答 1

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 可以保存图像:)

于 2015-09-06T00:27:32.047 回答