【问题标题】:How to save image in binary formate into database and also read data from database in android?如何将二进制格式的图像保存到数据库中并从android中的数据库中读取数据?
【发布时间】:2014-05-06 04:03:23
【问题描述】:

我想从相机拍摄图像,然后将图像保存在 SD 卡文件夹中,但路径保存在 SqLite 数据库中,并且还以二进制格式保存图像。同样,当从数据库读取时,在 ImageView 或 GridView 中显示图像。任何有关于此的资源或示例或教程链接。如果有人给我任何资源、示例或教程链接。这对我很有帮助。

【问题讨论】:

    标签: android sqlite android-imageview android-image


    【解决方案1】:

    您可以将您的任务分成三个独立的部分:

    1. Take a picture from camera
    2. Save it to a file and take the path
    3. Store the path from point two in your database
    

    对于第 1 部分和第 2 部分:

    final int IMAGE_FROM_CAMERA = 0;
    Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(takePicture, IMAGE_FROM_CAMERA);
    

    在 onActivityResult()...

    if (resultCode == RESULT_OK) {
    Uri selectedImage = imageReturnedIntent.getData();
    String path = getRealPathFromURI(selectedImage);
    }
    
    ...
    
    public String getRealPathFromURI(Uri contentUri) {
        String path = null;
        String[] proj = { MediaStore.MediaColumns.DATA };
        Cursor cursor = getContentResolver().query(contentUri, proj, null,
                null, null);
        if (cursor.moveToFirst()) {
            int column_index = cursor
                    .getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
            path = cursor.getString(column_index);
        }
        cursor.close();
        return path;
    }
    

    对于第 3 部分,只需将字符串 path 写入数据库中的相应字段即可。因为打开很容易,因为您有路径,只需从路径中打开一个流,进行一些转换并获取您的图像。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-18
      • 2011-12-21
      • 1970-01-01
      相关资源
      最近更新 更多