2016-06-07 63 views
-2

我想將照片保存在SQL數據庫的BLOB字段中。現在我正在做它保存一個字節數組,但爲應用程序工作太多努力...我改變了維度(BitmapFactory),以減少應用程序的工作,但圖像是一種馬賽克和缺乏解決方案。其實我正在用相機拍照並想保存它並在我的數據庫和應用程序中使用。我知道在數據庫中保存我的文件路徑(這將是外部SD卡)會更好,並在我的應用程序中檢索我需要的圖像,但找不到明確的教程來執行此操作。無論如何,我想保存我拍攝的照片的裁剪版本,以節省更多的內存。你有任何明確的教程分享,或有關代碼使用任何建議...謝謝你在前進在SQL中保存裁剪後的圖像

回答

0

試試這個

private void selectImage() { 

     final CharSequence[] options = { "Take Photo", "Choose from Gallery","Cancel" }; 

     AlertDialog.Builder builder = new AlertDialog.Builder(ProfileSettings.this); 
     builder.setTitle("Profile Picture!"); 
     builder.setItems(options, new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int item) { 
       if (options[item].equals("Take Photo")) { 
        Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
        startActivityForResult(takePicture, TAKEPIC); 
       } else if (options[item].equals("Choose from Gallery")) { 
        Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
        startActivityForResult(intent, PICKPIC); 

       } else if (options[item].equals("Cancel")) { 
        dialog.dismiss(); 
       } 
      } 
     }); 
     builder.show(); 
    } 


    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 

     if(resultCode==RESULT_OK) 
     { 
      if(requestCode==TAKEPIC) 
      { 
       Bitmap bitmap=(Bitmap) data.getExtras().get("data"); 
       Uri selectedImage =getImageUri(YOURACTIVITY.this,bitmap); 

       String localimagepath =getImagePath(YOURACTIVITY.this, selectedImage); 


      } 
      else if(requestCode==PICKPIC) 
      { 
       Uri selectedImage = data.getData(); 

       String localimagepath = getImagePath(YOURACTIVITY.this, data.getData()); 

      } 
     } 
    } 
    public Uri getImageUri(Context inContext, Bitmap inImage) { 
     ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
     inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes); 
     String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage,"title",null); 
     return Uri.parse(path); 
    } 


    public String getImagePath(Context context,Uri uri) { 
     String[] projection = { MediaStore.Images.Media.DATA }; 

     Cursor cursor =context.getContentResolver().query(uri,projection,null,null,null); //managedQuery(uri, projection, null, null, null); 
     if(cursor!=null) { 
      int column_index = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA); 
      cursor.moveToFirst(); 
      return cursor.getString(column_index); 
     } 
     else return null; 
    } 


    public void saveImagePath(String imagePath) 
    { 
     //push image path to db 
     DBHelpher dbHelpher=new DBHelpher(YOURACTIVITY.this); 
     dbHelpher.insertImagePath(imagePath); 
    } 

創建類的源碼保存在數據庫中的圖像路徑

public class DBHelpher extends SQLiteOpenHelper 
{ 

Context context; 
    public final String TABLE_SAMPLE="test"; 
     public final String COLUMN_ID="id"; 
    public final String COLUMN_IMAGEPATH="imagepath"; 

    public final String CREATE_TABLE__SAMPLE_SQL="create table " + TABLE_SAMPLE 
      + " (" + COLUMN_ID + " integer primary key autoincrement, " 
      + COLUMN_IMAGEPATH + " text " 
      + ");"; 


    public DBHelpher(Context context) 
    { 
     super(context, "DATABASENAME.db", null, 1); 
     this.context=context; 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 

     db.execSQL(CREATE_TABLE__SAMPLE_SQL); 


    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 

    } 


    public void insertImagePath(String imagePath) 
    { 

     SQLiteDatabase database = this.getWritableDatabase(); 
     ContentValues values = new ContentValues(); 
     values.put(COLUMN_IMAGEPATH,imagePath); 
     database.insert(TABLE_SAMPLE, null, values); 


    } 


} 

從您想要拍照的地方調用selectImage方法。