使用MediaStore.ACTION_IMAGE_CAPTURE,拍照到指定目录

  1. 示例代码如下:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (intent.resolveActivity(getPackageManager()) != null) {
        ContentValues contentValues = new ContentValues(2);
        String filePath = CommonUtils.getOutputMediaFile().getPath();//要保存照片的绝对路径
				
        contentValues.put(MediaStore.Images.Media.DATA, filePath);
        //如果想拍完存在系统相机的默认目录,改为
        //contentValues.put(MediaStore.Images.Media.DISPLAY_NAME, "111111.jpg");
        
        contentValues.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
        mPhotoUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);

        intent.putExtra(MediaStore.EXTRA_OUTPUT, mPhotoUri);
        startActivityForResult(intent, 111);
    }
  1. 可以通过如下方法,获取该图片的其他信息,比如ID等
String[] projection = {
            MediaStore.MediaColumns._ID,
            MediaStore.Images.ImageColumns.ORIENTATION,
            MediaStore.Images.Media.DATA
            };
Cursor c = getContentResolver().query(mPhotoUri, projection, null, null, null);
c.moveToFirst();
//mPhotoUri  参见上文示例代码

获取文件的详细名字 String photoFileName = c.getString(c.getColumnIndexOrThrow(MediaStore.Images.Media.DATA))

  1. 拍了一张照片,可是查Media数据库,查不到,可以试试如下
final String[] SCAN_TYPES = {"image/jpeg"};
MediaScannerConnection.scanFile(mContext, new String[]{file.getPath()}, SCAN_TYPES, null);

参考: http://stackoverflow.com/questions/1910608/android-action-image-capture-intent https://github.com/deepwinter/AndroidCameraTester

你可能感兴趣的:(使用MediaStore.ACTION_IMAGE_CAPTURE,拍照到指定目录)