将图像保存在getter和setter中,然后保存在SQLite数据库中

时间:2014-07-23 11:23:04

标签: android

我想将图像保存在getter setter中,然后保存在Sqlite Database.Please建议任何存储Bitmap图像的解决方案?

2 个答案:

答案 0 :(得分:0)

你必须创建一个这样的表tutoriel:

http://hmkcode.com/android-simple-sqlite-database-tutorial/

并且用于存储图像使用文件的URI部分。 (当您从画廊中选择一张照片或拍照时,您可以获得Uri)。在了解了Uri后,您可以将其放入数据库中。

说它是否有帮助。如果您有更多问题,请问:)

答案 1 :(得分:0)

将资源图像保存到SQLite数据库中:

Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG, 100, bos);
byte[] img = bos.toByteArray();

db = myDatabase.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("image", img);
db.insert(TABLE_IMAGE, null, cv);

为了从相机或图库中选择图像然后保存到SQLite数据库,我建议只保存图像的路径以节省空间。

使用以下代码,拍摄的图像也会保存到您的图库中的My App's Images,因此每当您打开图库时,您都可以看到哪些图片是从您的应用中拍摄的:

private static String imageFilePath;

private static final int SELECT_PICTURE_REQUEST_CODE = 0;
private static final int CAPTURE_IMAGE_REQUEST_CODE = 1;

// Choose picture from Gallery
Intent selectPictureIntent = Intent
                    .createChooser(intent, "Select Picture");
startActivityForResult(selectPictureIntent, SELECT_PICTURE_REQUEST_CODE);

// Take new picture
Intent captureImageIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String fileUri = getOutputMediaFileUri();
captureImageIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(captureImageIntent, CAPTURE_IMAGE_REQUEST_CODE);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == SELECT_PICTURE_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            Uri selectedImageUri = data.getData();
            String selectedImagePath = getPath(selectedImageUri);
            // save selectedImagePath to your database
            finish();
    }

    if (requestCode == CAPTURE_IMAGE_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            addImageToGallery(imageFilePath, context);
            // save imageFilePath to your database
            finish();
    }
}

实施这些方法:

public String getPath(Uri uri) {
    String[] projection = { MediaColumns.DATA };
    Cursor cursor = getContentResolver().query(uri, projection, null, null,
            null);
    if (cursor != null) {
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
        String filePath = cursor.getString(columnIndex);
        cursor.close();
        return filePath;
    } else
        return uri.getPath();
}

/** Create a file Uri for saving an image*/
private static Uri getOutputMediaFileUri() {
    return Uri.fromFile(getOutputMediaFile());
}


/** Create a File for saving an image or video */
private static File getOutputMediaFile() {
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.

    File mediaStorageDir = new File(
            Environment
                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
            "My App's Images");
    // This location works best if you want the created images to be shared
    // between applications and persist after your app has been uninstalled.

    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d("My App", "failed to create directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
            .format(new Date());
    File mediaFile;
    imageFilePath = mediaStorageDir.getPath() + File.separator + "IMG_"
                + timeStamp + ".jpg";
    mediaFile = new File(imageFilePath);
    return mediaFile;
}

public static void addImageToGallery(final String filePath,
        final Context context) {
    ContentValues values = new ContentValues();

    values.put(Images.Media.DATE_TAKEN, System.currentTimeMillis());
    values.put(Images.Media.MIME_TYPE, "image/jpeg");
    values.put(MediaStore.MediaColumns.DATA, filePath);

    context.getContentResolver().insert(Images.Media.EXTERNAL_CONTENT_URI,
            values);
}