2012-08-08 56 views
0

我有這樣的代碼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) 
+0

你能後的例外是什麼,堆棧跟蹤? – Liron 2012-08-08 17:24:25

+0

@Nonyck而不是發佈所有的內容發佈您的logcat和你得到錯誤的類 – Akshay 2012-08-08 17:24:29

+0

對不起,這裏是我的logcat,它在米安類的錯誤 – Nonyck 2012-08-08 17:34:59

回答

0

您正在使用的CursorAdapter。從文檔:

光標必須包括一個名爲「_id」的列或這個類將不會 工作。

你可以改變你的架構以包括「_id」在你的數據庫,或者你可以別名SQL內置的ROWID作爲_id在查詢:

return (getReadableDatabase().rawQuery("SELECT rowid _id, imageblob FROM Image", 
       null)); 
+0

幾乎完成的應用程序現在不瘋了,但圖像不顯示日誌告訴這個 - > 08-08 18:54:26.352:D/skia(1186):--- SkImageDecoder :: Factory返回null – Nonyck 2012-08-08 18:55:39

+0

問題很可能是你的調用:'BM = BitmapFactory.decodeByteArray(bytes,0, 0);'你指定的字節長度爲0,它應該是'BM = BitmapFactory.decodeByteArray(bytes,0,bytes.length);' – 2012-08-08 19:13:50

+0

THX ALOT !!!!!! – Nonyck 2012-08-08 19:27:13

相關問題