将图片从android插入数据库

时间:2013-11-20 10:01:18

标签: java android mysql

在我的项目中,我遇到了从android到数据库插入图片的问题。 我有成功插入画廊移动图片但不工作从相机移动插入。 这是我的源代码

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        if (requestCode == REQUEST_CAMERA) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaColumns.DATA };
            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String filePath = cursor.getString(columnIndex);
            cursor.close();
            pathToOurFile = filePath;
            format = filePath.substring(filePath.lastIndexOf(".") + 1,
                    filePath.length());
            this.imGambar.setImageBitmap((Bitmap) data.getExtras().get(
                    "data"));

        } else if (requestCode == SELECT_FILE) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };
            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String filePath = cursor.getString(columnIndex);
            cursor.close();
            pathToOurFile = filePath;
            format = filePath.substring(filePath.lastIndexOf(".") + 1,
                    filePath.length());
            this.imGambar
                    .setImageBitmap(BitmapFactory.decodeFile(filePath));
        }
    }
}

1 个答案:

答案 0 :(得分:0)

此方法无法正常处理请求相机活动结果中的Uri。你最好用:

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
mImageCaptureUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); 
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
startActivityForResult(cameraIntent, CAMERA_REQUEST);

在onActivityResult中:

String path = mImageCaptureUri.toString();

其中

Uri mImageCaptureUri ;

是一个全局变量。

希望它有所帮助...