2013-06-19 7 views
1

나는 사진을 SQLite에 blob로 저장하려고한다. mCurrentMediaPath는 사진이 저장 될 현재 경로입니다. 이제 사진을 찍은 후 버튼을 눌러 저장해야합니다 (의도 한 것 같아요).안드로이드 데이터베이스에 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;   
    }  
} 

어디에서 삽입해야합니까? DBA는 데이터베이스 클래스의 객체이다

 //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

저장하여 데이터베이스에서 이미지의 경로 대신 방울 – 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); 

을 비트 맵을 만듭니다.

같은 데이터베이스 클래스의 테이블을 만들 :

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"메소드에서 가장 많이 사용하는 방법은 무엇입니까? 비트 맵 변수는 여전히 null로 돌아옵니다. – user2224135