2013-06-19 94 views
1

我試圖將照片保存爲SQLite中的blob(不僅僅是引用它)。 mCurrentMediaPath是照片將被存儲的當前路徑。現在,我需要保存到數據庫後圖片拍攝和保存按鈕按下(我猜意圖後)。如何將android數據庫中的照片保存爲blob

public Uri insert(byte[] image) { 
    return getContentResolver().insert(MyContentProvider.CONTENT_URI7, createContentValues(image)); 
} 
private ContentValues createContentValues(byte[] image) { 
    ContentValues docsInsert = new ContentValues(); 
    docsInsert.put(Db.COLUMN_FILETYPE, "PHOTO"); 
    docsInsert.put(Db.COLUMN_NAME, mCurrentMediaPath); 
    docsInsert.put(Db.COLUMN_FILE, image); 
    return docsInsert; 
} 
// convert from bitmap to byte array 
public byte[] getBytesFromBitmap(Bitmap bitmap) { 
    ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
    bitmap.compress(CompressFormat.JPEG, 70, stream); 
    return stream.toByteArray(); 
} 


private void dispatchMediaIntent(int actionCode) { 
    switch(actionCode) { 
    case ACTION_TAKE_PHOTO: 
     Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     File f = null; 
     try { 
      f = setUpPhotoFile(ACTION_TAKE_PHOTO); 
      mCurrentMediaPath = f.getAbsolutePath(); 
      takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f)); 
     } catch (IOException e) { 
      e.printStackTrace(); 
      f = null; 
      mCurrentMediaPath = null; 
     } 
     startActivityForResult(takePictureIntent, actionCode); 
     break; 
    case ACTION_TAKE_VIDEO: 
     Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE); 
     startActivityForResult(takeVideoIntent, actionCode); 
     break; 
    default: 
     break;   
    }  
} 

我應該在哪裏實施插入?

 //SAVING TO DATABASE 
     BitmapFactory.Options bmOptions = new BitmapFactory.Options(); 
     bmOptions.inJustDecodeBounds = true; 
     BitmapFactory.decodeFile(mCurrentMediaPath, bmOptions); 
     Bitmap bitmap = BitmapFactory.decodeFile(mCurrentMediaPath, bmOptions); 

     insert(getBytesFromBitmap(bitmap)); 
+0

存儲圖像的數據庫的路徑,而不是存儲圖像爲BLOB – Raghunandan

+0

請閱讀我的文章的第一行。 – user2224135

+0

如果你有大量的圖像存儲在數據庫中佔用更多的空間。相反,您可以存儲圖像的路徑。 – Raghunandan

回答

1

創建位圖圖像的再

Bitmap bmp = BitmapFactory.decodeStream(new FileInputStream(f), null, null); 
ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream); 
byte[] byteArray = stream.toByteArray(); 

dba.open(); 

dba.insertPhoto(byteArray); 

其中DBA是數據庫類的對象。

創建數據庫類如表:

private static final String CREATETABLE_PHOTO = "create table eqpphoto("EImage BLOB " + ");"; 

public static final String TABLE_PHOTO = "eqpphoto"; 

public long insertPhoto(byte[] EImage) { 

    try { 
     System.out.println("Function call : "); 
     ContentValues values = new ContentValues(); 

     values.put(EIMAGE, EImage); 
     return db.insert(TABLE_PHOTO, null, values); 
    } catch (Exception e) { 
     e.printStackTrace(); 
     return 0; 
    } 
} 
+0

你是如何聲明「bmp」的? – user2224135

+0

檢查我編輯的答案.. – Riser

+0

關於我的大部分,在「dispatchMediaIntent」方法中你會實現它嗎?由於Bitmap變量仍然爲空。 – user2224135

相關問題