0

我有这个代码

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

1 回答 1

0

您正在使用 CursorAdapter。从文档中:

游标必须包含名为“_id”的列,否则此类将不起作用。

您可以更改架构以在数据库中包含“_id”,也可以在查询中将 SQL 的内置 rowid 别名为 _id:

return (getReadableDatabase().rawQuery("SELECT rowid _id, imageblob FROM Image",
                null));
于 2012-08-08T18:25:49.137 回答