将裁剪后的图像保存在 SQL

Save cropped image in SQL

我想在我的 SQL 数据库中的 BLOB 字段中保存一张照片。现在我正在做它保存一个字节数组,但应用程序工作太多的努力......我改变了尺寸(BitmapFactory)以减少应用程序工作,但图像是一种马赛克并且缺乏分辨率。实际上我正在用相机拍照并想保存它并在我的数据库和应用程序中使用。我知道最好将我的文件路径(将是外部 SD 卡)保存在数据库中并在我的应用程序中检索我需要的图像,但我找不到清晰的教程来做到这一点。无论如何,我想保存我拍摄的照片的裁剪版本,以节省更多内存。您是否有任何明确的教程可以分享,或者关于代码使用的任何建议...提前谢谢您

试试这个

 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);
    }

为sqlite创建class以在数据库中保存图像路径

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方法。