我有這樣的代碼Android商店的攝像機圖像,並在視圖中恢復它們
public class MainActivity extends Activity {
private static final int CAMERA_REQUEST = 1888;
public ImageView imageView;
Imagehelper help = new Imagehelper(this);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.imageView = (ImageView) this.findViewById(R.id.imageView1);
ListView lv = (ListView) findViewById(R.id.fotos);
Cursor valores = help.getAll();
startManagingCursor(valores);
ItemAdapter itemAdapter = new ItemAdapter(this, valores);
lv.setAdapter(itemAdapter);
Button B = (Button) this.findViewById(R.id.camera);
B.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
help.insert(byteArray);
}
}
}
適配器類
public class ItemAdapter extends CursorAdapter {
private LayoutInflater mLayoutInflater;
private Context mContext;
public ItemAdapter(Context context, Cursor c) {
super(context, c);
mContext = context;
mLayoutInflater = LayoutInflater.from(context);
}
@Override
public void bindView(View v, Context context, Cursor c) {
Bitmap BM;
c.moveToFirst();
byte[] bytes = c.getBlob(c.getColumnIndex("imageblob"));
BM = BitmapFactory.decodeByteArray(bytes, 0, 0);
ImageView img = (ImageView) v.findViewById(R.id.imagenes);
img.setImageBitmap(BM);
}
@Override
public View newView(Context arg0, Cursor arg1, ViewGroup parent) {
View v = mLayoutInflater.inflate(R.layout.imagen_row, parent, false);
return v;
}
}
和數據庫
public class Imagehelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "abhi.db";
private static final int SCHEMA_VERSION = 3;
public Imagehelper(Context context) {
super(context, DATABASE_NAME, null, SCHEMA_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE Image(_id INTEGER PRIMARY KEY AUTOINCREMENT,imageblob BLOB);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public Cursor getAll() {
return (getReadableDatabase().rawQuery("SELECT imageblob FROM Image",
null));
}
public void insert(byte[] bytes) {
ContentValues cv = new ContentValues();
cv.put("imageblob", bytes);
Log.e("inserted", "inserted");
getWritableDatabase().insert("Image", "imageblob", cv);
}
public byte[] getImage(Cursor c) {
return (c.getBlob(1));
}
}
但是我跑的時候此代碼的應用程序崩潰。我在做什麼壞?
日誌貓跟蹤的部分是,
08-08 17:32:55.779: E/AndroidRuntime(746): FATAL EXCEPTION: main
08-08 17:32:55.779: E/AndroidRuntime(746): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pruebascamarasqlite/com.example.pruebascamarasqlite.MainActivity}: java.lang.IllegalArgumentException: column '_id' does not exist
08-08 17:32:55.779: E/AndroidRuntime(746): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
08-08 17:32:55.779: E/AndroidRuntime(746): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
你能後的例外是什麼,堆棧跟蹤? – Liron 2012-08-08 17:24:25
@Nonyck而不是發佈所有的內容發佈您的logcat和你得到錯誤的類 – Akshay 2012-08-08 17:24:29
對不起,這裏是我的logcat,它在米安類的錯誤 – Nonyck 2012-08-08 17:34:59