2011-12-02 86 views
0

我很努力地在sqlite上保存圖像。 我想將圖像保存爲sqlite上的blob。 我不知道如何聲明imageViewbyte[],因爲我在dbAdapter上使用插入方法。 將圖像保存到數據庫是否好方法?有人說將文件路徑url保存到數據庫中會更好。如果更好,請給我一些你的手。 我真的越來越難。 非常感謝。從相機或圖庫在sqlite上保存圖像

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{ 
    super.onActivityResult(requestCode, resultCode, data); 
    if (resultCode != RESULT_OK) return; 
     switch (requestCode) 
     { 
     case PICK_FROM_CAMERA: 
     Bundle extras = data.getExtras(); 
     ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
     Bitmap selectedImage = (Bitmap) extras.get("data"); 
     selectedImage = Bitmap.createScaledBitmap(selectedImage, 200, 250, false); 
     selectedImage.compress(CompressFormat.PNG, 0, bos); 
     mImageView.setImageBitmap(selectedImage); 
     break; 

     case PICK_FROM_GALLERY: 
      Uri selectedImageUri = data.getData(); 
      selectedImagePath = getPath(selectedImageUri); 
      System.out.println("Image Path : " + selectedImagePath); 
      mImageView.setImageURI(selectedImageUri); 
     break; 
     } 
} 

protected void onSaveInstanceState(Bundle outState) 
{ 
    super.onSaveInstanceState(outState); 
    saveState(); 
} 

private void saveState() 
{ 
    String name = (String) nameEdit.getText().toString(); 
    String category = (String) categoryEdit.getText().toString(); 
    String expired_date = (String) expired_Date_Btn.getText().toString(); 
    //byte[] image = (byte[]) mImageView... I HAVE TO DO SOME CODING HERE.. 
    if(mRowId == null) 
    { 
     long id = mDbHelper.insertItem(category, name, expired_date, image); 

     if(id>0) 
     { 
      mRowId = id; 
     }   
    } 
    else 
    { 
     mDbHelper.updateItem(mRowId, category, name, expired_date, image); 
    } 
} 

將對DBAdapter類

public long insertItem(String category, String name, String expired_date, byte[] image) 
{ 
    ContentValues initialValues = new ContentValues(); 
    initialValues.put(KEY_CATEGORY, category); 
    initialValues.put(KEY_NAME, name); 
    initialValues.put(KEY_EXPIRED_DATE, expired_date); 
    initialValues.put(KEY_IMAGE, image); 
    return db.insert(DATABASE_TABLE, null, initialValues); 
} 

回答

1

試着這樣做:

ConvertDrawableToByteArray(mImageView.getDrawable() 

隨着ConvertDrawabelToByteArray定義是這樣的:

public static byte[] ConvertDrawableToByteArray(Drawable drawableResource) { 

    Bitmap imageBitmap = ((BitmapDrawable) drawableResource).getBitmap(); 
    ByteArrayOutputStream imageByteStream = new ByteArrayOutputStream(); 
    imageBitmap.compress(Bitmap.CompressFormat.PNG, 100, imageByteStream); 
    byte[] imageByteData = imageByteStream.toByteArray(); 
    return imageByteData; 
} 

雖然我認爲你應該能夠得到字節超出位地圖。所有這一切說,在sql lite中存儲文件路徑可能會更有效,但是,如果用戶刪除了圖像,則必須在應用程序中處理該圖像。

+0

感謝您的回覆,這是爲我好。但是,仍然我的應用程序不能處理數據庫與圖像:( – wholee1

+0

你可以展開你的意思是它不會處理圖像? – Chris

+0

我認爲DbAdapter有問題,因爲圖像列無法得到它。在dbAdapter中,我創建圖像列爲blob類型,但是,sqlite目前無法獲得blob列,我不知道它是什麼問題,當我運行應用程序時,它不能從數據庫中獲取項目,然後我仍然在爲保存圖像而苦苦掙扎給出了答案.. – wholee1

2

對於有關相機問題的其他部分,這段代碼將會很有幫助。

buttonClick = (Button) findViewById(R.id.buttonClick); 
buttonClick.setOnClickListener(new OnClickListener() { 
    public void onClick(View v) { 
     preview.camera.takePicture(shutterCallback, rawCallback, jpegCallback); 
    } 
}); 

myDBHelper = new MyDBHelper(this); 
readDatabase(); 
} 

protected void newTab(String label, Drawable icon, int page) { 
    TabSpec tabSpec = tabHost.newTabSpec(label); 
    tabSpec.setIndicator(label,icon); 
    tabSpec.setContent(page); 
    tabHost.addTab(tabSpec); 
} 

// Called when shutter is opened 
ShutterCallback shutterCallback = new ShutterCallback() { 
    public void onShutter() { 
    } 
}; 

// Handles data for raw picture 
PictureCallback rawCallback = new PictureCallback() { 
    public void onPictureTaken(byte[] data, Camera camera) { 
    } 
}; 

// Handles data for jpeg picture 
PictureCallback jpegCallback = new PictureCallback() { 
    public void onPictureTaken(byte[] data, Camera camera) { 

     SQLiteDatabase db = myDBHelper.getReadableDatabase(); 
     ContentValues values = new ContentValues(); 
     values.put("image", data); 
     db.insert("storedImages", "tag", values); 
     preview.camera.startPreview(); 
    } 
}; 

protected void readDatabase() { 
    TextView info = (TextView) findViewById(R.id.info); 
    info.setText("Integer.toString(cursor.getCount())"); 
    SQLiteDatabase db = myDBHelper.getReadableDatabase(); 
    Cursor cursor = db.rawQuery("SELECT * FROM storedImages ;", null); 

    info.setText(Integer.toString(cursor.getCount())); 

    if (cursor.getCount()>0) { 
     cursor.moveToNext(); 
     ImageView image = (ImageView) findViewById(R.id.image); 
     byte[] data = cursor.getBlob(cursor.getColumnIndex("image")); 
     image.setImageBitmap(BitmapFactory.decodeByteArray(data, 0, data.length)); 
    } 
} 

} 

Have a look at this

+1

你可以檢查這個鏈接的一些有用的建議關於,什麼會更好的方式來保存圖像在SQLite數據庫.http://stackoverflow.com/questions/8119162/how -to-STORE-圖像功能於源碼數據庫和 - 顯示它們 – mH16