<span style="white-space:pre"> </span>intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File dirPath = new File(Constants.SYS_PHOTO_PATH); // 图片路径要自己提前定义好
if(!dirPath.exists()) {
dirPath.mkdirs();
}
String fileName = "IMG_" + new DateFormat().format("yyyyMMdd_hhmmss",Calendar.getInstance(Locale.CHINA)) + ".jpg";
String filePath = Constants.SYS_PHOTO_PATH + fileName;
LogUtils.i("startActivity", "Take photo, filepath = " + filePath);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(filePath))); // 这样就将文件的存储方式和uri指定到了Camera应用中
intent.putExtra("file", filePath);
intent.putExtra("filename", fileName);
AppConfig.setCurrentPhotoUri(activity, fileName); // 将文件名存储为全局变量,以便在回调时读取,存储方法自己写
activity.startActivityForResult(intent, 0);
// 回调方法
/**
* 处理相机保存图片
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
LogUtils.i(TAG, "Take photo start");
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
LogUtils.i(TAG, "Take photo success, save image");
String fileName = AppConfig.getCurrentPhotoUri(this);
String filePath = Constants.SYS_PHOTO_PATH + fileName;
Uri localUri = Uri.fromFile(new File(filePath));
Intent localIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, localUri);
sendBroadcast(localIntent);
} else {
LogUtils.i(TAG, "Take photo success, unsave");
}
}
上次用的方法有问题,修正后的方法如上面的两段代码所示,可实现在拉起摄像头拍照,并且系统图库可显示。
参考文档