我正在尝试将图像插入SQLite数据库并检索它。我认为数据插入成功,但程序在检索时停止。你能帮我么?。 Logcat显示 com.example.mypassword.Login $ 1.onClick(Login.java:51)
中的java.lang.NullPointerException我已附加代码以插入和检索图像。这是我的SQLiteOpenHelper类。
public class SQL extends SQLiteOpenHelper {
public static final String DB_NAME="New.db";
public static final String TABLE_NAME="Sample";
public static final int DB_VERSION=1;
public static final String NAME="Name";
public static final String NO="No";
public static final String IMG="Img";
public static final String PIXELX="PosX";
public static final String PIXELY="PosY";
Cursor cur;
byte[] blob;
String name;
String no;
int pixelx,pixely;
Bitmap bmp;
Context ctxt;
//private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
public SQL(Context context)
{
super(context,DB_NAME,null,1);
}
public void onCreate(SQLiteDatabase db)
{
db.execSQL("CREATE TABLE "+TABLE_NAME+"("+NAME +" TEXT NOT NULL "+","+ NO +" TEXT PRIMARY KEY ,"+IMG +" BLOB,"+PIXELX +" INTEGER, "+ PIXELY +" INTEGER);");
}
public void onUpgrade(SQLiteDatabase db,int old,int newversion)
{
db.execSQL("DROP TABLE IF EXISTS"+TABLE_NAME+";");
onCreate(db);
}
public void drop(SQLiteDatabase db)
{
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME+" ;");
}
public void open(SQLiteDatabase db)
{
db=this.getWritableDatabase();
}
public void close(SQLiteDatabase db)
{
db.close();
}
public void insert(SQLiteDatabase db,String name,String no,Bitmap ImgData,int pixelx,int pixely)
{
//db=this.getWritableDatabase();
ContentValues con=new ContentValues();
con.put(NAME, name);
con.put(NO, no);
con.put(IMG, Utility.getBytes(ImgData));
con.put(PIXELX, pixelx);
con.put(PIXELY, pixely);
db.insert(TABLE_NAME, null, con);
}
@SuppressLint("NewApi") public User retrieve(SQLiteDatabase db)
{
cur=db.query(true,TABLE_NAME, new String[]{NAME,NO,IMG,PIXELX,PIXELY},null, null,null, null,null, null,null);
//cur=db.query(TABLE_NAME, new String[]{IMG},null, null,null, null, null);
if(cur.moveToFirst())
{
name=cur.getString(cur.getColumnIndex(NAME));
no=cur.getString(cur.getColumnIndex(NO));
blob=cur.getBlob(cur.getColumnIndex(IMG));
pixelx=cur.getInt(cur.getColumnIndex(PIXELX));
pixely=cur.getInt(cur.getColumnIndex(PIXELY));
cur.close();
return new User(name,no,Utility.getPhoto(blob),pixelx,pixely);
}
cur.close();
return null;
}
public ArrayList<Cursor> getData(String Query){
//get writable database
SQLiteDatabase sqlDB = this.getWritableDatabase();
String[] columns = new String[] { "mesage" };
//an array list of cursor to save two cursors one has results from the query
//other cursor stores error message if any errors are triggered
ArrayList<Cursor> alc = new ArrayList<Cursor>(2);
MatrixCursor Cursor2= new MatrixCursor(columns);
alc.add(null);
alc.add(null);
try{
String maxQuery = Query ;
//execute the query results will be save in Cursor c
Cursor c = sqlDB.rawQuery(maxQuery, null);
//add value to cursor2
Cursor2.addRow(new Object[] { "Success" });
alc.set(1,Cursor2);
if (null != c && c.getCount() > 0) {
alc.set(0,c);
c.moveToFirst();
return alc ;
}
return alc;
} catch(SQLException sqlEx){
Log.d("printing exception", sqlEx.getMessage());
//if any exceptions are triggered save the error message to cursor an return the arraylist
Cursor2.addRow(new Object[] { ""+sqlEx.getMessage() });
alc.set(1,Cursor2);
return alc;
} catch(Exception ex){
Log.d("printing exception", ex.getMessage());
//if any exceptions are triggered save the error message to cursor an return the arraylist
Cursor2.addRow(new Object[] { ""+ex.getMessage() });
alc.set(1,Cursor2);
return alc;
}
}
}
这是我插入数据的代码
public class Register extends Activity {
ImageView img;
String picpath;
Bitmap bmp;
Canvas cnvs;
Random r;
int x,y,height,width;
ByteArrayOutputStream blob;
SQL sql;
Paint paint;
String name,no;
Uri data;
SQLiteDatabase db;
String[] filePath={MediaStore.Images.Media.DATA};;
Cursor cur;
TextView txt;
int colIndex;
byte[] imgData;
public Register()
{
}
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
img=(ImageView)findViewById(R.id.imageView1);
sql=new SQL(this);
db=sql.getWritableDatabase();
sql.drop(db);
sql.onCreate(db);
r=new Random();
paint=new Paint();
x=r.nextInt(100);
blob=new ByteArrayOutputStream();
Intent i=getIntent();
name=i.getStringExtra("name");
no=i.getStringExtra("no");
//Open Button
Button open=(Button)findViewById(R.id.button3);
open.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent gal_open=new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(gal_open,1);
}
});
txt=(TextView)findViewById(R.id.textView1);
Button shuffle=(Button)findViewById(R.id.button2);
shuffle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
bmp=Bitmap.createBitmap(img.getHeight(),img.getWidth(),Bitmap.Config.RGB_565);
cnvs=new Canvas(bmp);
height=img.getMeasuredHeight();
width=img.getMeasuredWidth();
x=r.nextInt(height);
y=r.nextInt(width);
paint.setColor(Color.RED);
txt.setText(x+","+y);
cnvs.drawBitmap(BitmapFactory.decodeFile(picpath), 0, 0, null);
cnvs.drawRect(x, y,x+20,y+20 , paint);
img.setImageBitmap(bmp);
}
});
Button ok= (Button)findViewById(R.id.button1);
txt=(TextView)findViewById(R.id.textView1);
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
txt.setText(x+"'"+y);
sql.insert(db,"Vignesh","9578323986",bmp,x,y);
}
});
}
public void onDraw()
{
}
public void onActivityResult(int requestCode,int resultCode,Intent intentData)
{
super.onActivityResult(requestCode, resultCode, intentData);
if(requestCode==1 && resultCode==RESULT_OK && intentData!=null)
{
bmp=Bitmap.createBitmap(img.getHeight(),img.getWidth(),Bitmap.Config.RGB_565);
cnvs=new Canvas(bmp);
//img.setImageBitmap(bmp);
height=img.getMeasuredHeight();
width=img.getMeasuredWidth();
x=r.nextInt(height);
y=r.nextInt(width);
paint.setColor(Color.RED);
data=intentData.getData();
cur=getContentResolver().query(data,filePath,null,null,null );
cur.moveToFirst();
colIndex=cur.getColumnIndex(filePath[0]);
picpath=cur.getString(colIndex);
cur.close();
txt=(TextView)findViewById(R.id.textView1);
txt.setText(x+","+y);
cnvs.drawBitmap(BitmapFactory.decodeFile(picpath), 0, 0, null);
cnvs.drawRect(x, y,x+20,y+20 , paint);
img.setImageBitmap(bmp);
}
}
}
这是我的Utility类代码
public class Utility {
public static byte[] getBytes(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 100, stream);
return stream.toByteArray();
}
// convert from byte array to bitmap
public static Bitmap getPhoto(byte[] image) {
return BitmapFactory.decodeByteArray(image, 0, image.length);
}
}
这是我的用户类代码
public class User {
String name,no;
int pixelx,pixely;
public Bitmap bmp;
public User(String n,String num,Bitmap img,int pixel1,int pixel2)
{
name=n;
no=num;
bmp=img;
pixelx=pixel1;
pixely=pixel2;
}
public Bitmap retBmp()
{
return bmp;
}
public String Name()
{
return name;
}
public String No()
{
return no;
}
public int PixelX()
{
return pixelx;
}
public int PixelY()
{
return pixely;
}
}
这是onClick,我得到NullPointerException
public class Login extends Activity {
SQL sql;
User user;
String name;
TextView no;
ImageView img;
int imgx,imgy;
View touchlistener;
SQLiteDatabase db;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
sql=new SQL(this);
sql.open(db);
img=(ImageView)findViewById(R.id.imageView1);
// no=(TextView)findViewById(R.id.textView1);
//name=no.getText().toString();
**Button getimg=(Button)findViewById(R.id.button1);
getimg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
user =sql.retrieve(db);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
img.setImageBitmap(user.retBmp());
}
});**
}
}