收到错误消息,无法从sqlite db中检索图像

时间:2012-05-25 12:13:43

标签: android sqlite

  

可能重复:
  cannot retrive the information from sqlite

在Checkdata.java中获取错误,因为图标无法解析或不是字段 我不想通过资源文件夹插入图像我想从存储卡中选择图像,(我已经插入代码如何从存储卡中选择图像)任何人都可以帮助我

这是我的SaveData.java

public class SaveData extends Activity implements OnClickListener {  
    private DataManipulator dh;     
    static final int DIALOG_ID = 0;

    private Bitmap bmp;
    private Uri mImageCaptureUri;
    private ImageView mImageView;   

    private static final int PICK_FROM_CAMERA = 1;
    private static final int PICK_FROM_FILE = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.addname);

        View button1Click=findViewById(R.id.btn_choose);
        button1Click.setOnClickListener(this);
        View button2Click = findViewById(R.id.Button01add);
        button2Click.setOnClickListener(this);
        View button3Click = findViewById(R.id.Button01home);
        button3Click.setOnClickListener(this);  
    }

    public SaveData(Bitmap b) {
        bmp=b;
    }

    public Bitmap getBitmap() { return bmp; }

    public void onClick(View v){


        switch(v.getId()){

        case R.id.Button01add:
            View editText1 = (EditText) findViewById(R.id.name);
            String myEditText1=((TextView) editText1).getText().toString();

            this.dh = new DataManipulator(this);
//          this.dh.insert(myEditText1);

            showDialog(DIALOG_ID);
            break;

        case R.id.Button01home: 
            Intent i2 = new Intent(this, CheckData.class);  
            startActivity(i2);
            break;

        }
    }
}

这是我的DataManipulator.java

public class DataManipulator {
    private static final  String DATABASE_NAME = "mydatabase.db";
    private static final int DATABASE_VERSION = 1;
    static final String TABLE_NAME = "newtable";

    public static final String KEY_IMG = "image";
    static SQLiteDatabase mDb;
    private DatabaseHelper mDbHelper;

    private static final String FRUITS_TABLE="fruits";

    private static final String CREATE_FRUITS_TABLE= "create table" +FRUITS_TABLE+" (" +KEY_IMG+" blob not null)";

    private final Context mCtx;

    private static class DatabaseHelper extends SQLiteOpenHelper {
        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        public void onCreate(SQLiteDatabase db) {
            db.execSQL(CREATE_FRUITS_TABLE);
        }

        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS "+FRUITS_TABLE);
            onCreate(db);
        }
    }

    public void Reset() { mDbHelper.onUpgrade(this.mDb, 1, 1); }

    public  DataManipulator(Context ctx) {
        mCtx = ctx;
        mDbHelper = new DatabaseHelper(mCtx);
    }

    public DataManipulator open() throws SQLException {
        mDb = mDbHelper.getWritableDatabase();
        return this;
    }

    public void close() { mDbHelper.close(); }

    public void createFruitEntry(SaveData fruit) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        fruit.getBitmap().compress(Bitmap.CompressFormat.PNG, 100, out);
        ContentValues cv = new ContentValues();
        cv.put(KEY_IMG, out.toByteArray());            
        mDb.insert(FRUITS_TABLE, null, cv);
    }

    public SaveData getFirstFruitFromDB() throws SQLException {
        Cursor cur = mDb.query(true,
                               FRUITS_TABLE,
                               new String[] {KEY_IMG},null, null,null, null, null, null);
        if(cur.moveToFirst()) {
            byte[] blob = cur.getBlob(cur.getColumnIndex(KEY_IMG));
            Bitmap bmp = BitmapFactory.decodeByteArray(blob, 0, blob.length);
            cur.close();
            return new SaveData(bmp);
        }
        cur.close();
        return null;
    }    
}

这是我的CheckData.java

public class CheckData extends ListActivity  {     

    private DataManipulator DbHelper;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LinearLayout layout = new LinearLayout(this);
        ImageView image = new ImageView(this);
        TextView text = new TextView(this);

        DbHelper = new DataManipulator(this);

        SaveData testSaveData = new SaveData(BitmapFactory.decodeResource(getResources(), R.drawable.icon), "Icon", 100, 0);

        DbHelper.open();
        DbHelper.createFruitEntry(testSaveData);
        DbHelper.close();

        testSaveData = null;

        DbHelper.open();
        testSaveData = DbHelper.getFirstFruitFromDB();
        DbHelper.close();

        image.setImageBitmap(testSaveData.getBitmap());

        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
                                        LinearLayout.LayoutParams.WRAP_CONTENT,
                                        LinearLayout.LayoutParams.WRAP_CONTENT);

        setContentView(layout);
        addContentView(image, params);
        addContentView(text, params);
    }
}

0 个答案:

没有答案