我是Android应用开发的新手。对于我的一个项目,我需要将图像存储在数据库中。所以这就是我的所作所为。
package com.img.db;
import java.io.ByteArrayOutputStream;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
public class ImageDb extends Activity {
/** Called when the activity is first created. */
DatabaseHelper dbHelper;
Bitmap[] images = new Bitmap [3];
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = this.getResources();
images[0] = BitmapFactory.decodeResource(res,R.drawable.orange);
//images[0] = Bitmap.createScaledBitmap(images[0], 70, 70, true) ;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
images[0].compress(Bitmap.CompressFormat.PNG, 100, baos);
//bm is the bitmap object
byte[] b = baos.toByteArray();
String name;
name="sample_1";
MyImage img = new MyImage(name,b);
dbHelper = new DatabaseHelper(this);
dbHelper.AddImage(img);
int n = dbHelper.getImageCount();
Log.i("ImageCount","n == "+n);
}
}
和MyImage.java:
package com.img.db;
import java.io.ByteArrayOutputStream;
import java.sql.Blob;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
public class MyImage {
int _id;
String _name;
byte[] _img;
public MyImage(String Name,byte[] Img)
{
this._name=Name;
this._img=Img;
}
public int getID()
{
return this._id;
}
public void SetID(int ID)
{
this._id=ID;
}
public String getName()
{
return this._name;
}
public byte[] getImg()
{
return this._img;
}
public void setName(String Name)
{
this._name=Name;
}
public void setImg(Bitmap bitmap)
{
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0, outputStream);
this._img = outputStream.toByteArray();
}
}
和DatabaseHelper.java:
package com.img.db;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DatabaseHelper extends SQLiteOpenHelper {
static final String dbName="imageDB";
static final String imgTable="MyImage";
static final String colID="ImageID";
static final String colName="ImageName";
static final String colbytes ="Imagebytes";
public DatabaseHelper(Context context) {
super(context, dbName, null,33);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
Log.i("in here", "creating database");
db.execSQL("CREATE TABLE "+ imgTable+" ("+ colID + " INTEGER PRIMARY KEY AUTOINCREMENT, "+ colbytes + "BLOB, "
+ colName + " TEXT)");
//Inserts pre-defined departments
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS "+ imgTable);
onCreate(db);
}
void AddImage(MyImage img)
{
Log.i("in here", "adding image");
SQLiteDatabase db= this.getWritableDatabase();
if (db==null)
{
Log.i("nulll", "mnllllsg");
}
ContentValues cv=new ContentValues();
cv.put(colName, img.getName());
cv.put(colbytes, img.getImg());
cv.put(colID, 1);
db.insert(imgTable, colID, cv);
db.close();
}
int getImageCount()
{
SQLiteDatabase db=this.getWritableDatabase();
Cursor cur= db.rawQuery("Select * from "+imgTable, null);
int x= cur.getCount();
cur.close();
return x;
}
Cursor getAllImages()
{
SQLiteDatabase db=this.getWritableDatabase();
//Cursor cur= db.rawQuery("Select "+colID+" as _id , "+colName+", "+colAge+" from "+employeeTable, new String [] {});
Cursor cur= db.rawQuery("SELECT * FROM "+imgTable,null);
return cur;
}
public int UpdateImg(MyImage img)
{
SQLiteDatabase db=this.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put(colName, img.getName());
cv.put(colbytes, img.getImg());
return db.update(imgTable, cv, colID+"=?", new String []{String.valueOf(img.getID())});
}
public void DeleteImg(MyImage img)
{
SQLiteDatabase db=this.getWritableDatabase();
db.delete(imgTable,colID+"=?", new String [] {String.valueOf(img.getID())});
db.close();
}
}
错误记录
I/in here ( 635): adding image
I/SqliteDatabaseCpp( 635): sqlite returned: error code = 1, msg = table MyImage has no column named Imagebytes, db=/data/data/com.img.db/databases/imageDB
E/SQLiteDatabase( 635): Error inserting ImageName=sample_1 ImageID=1 Imagebytes=[B@406ed320
E/SQLiteDatabase( 635): android.database.sqlite.SQLiteException: table MyImage has no column named Imagebytes: , while compiling: INSERT INTO MyImage(ImageName,ImageID,Imagebytes) VALUES (?,?,?)
请在代码中指出我的错误或给我一些指示,以便我可以得到一些帮助。
任何帮助都会很棒:)
答案 0 :(得分:2)
你在这里忘记了一个空白区域
+ colbytes + "BLOB,
将其更改为
+ colbytes + " BLOB,
答案 1 :(得分:1)
错误非常自我解释,您正在尝试访问不存在的列表:
table MyImage has no column named Imagebytes