private ArrayListmImageList;private static final int REQUEST_CAMERA = 5; //相机拍照标记
private static final int CAMERA_REQUEST_CODE = 1;/*** 拍照获取照片*/
protected voidselectPicFromCamera() {if (!SDCardUtils.isSDCardEnable()){//不存在sd卡
Toast.makeText(this,R.string.sd_disable,Toast.LENGTH_SHORT).show();return;
}//android 6.0之后需要动态申请权限
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)!=PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,newString[]{Manifest.permission.CAMERA}, CAMERA_REQUEST_CODE);
}
Intent cameraIntent= newIntent(MediaStore.ACTION_IMAGE_CAPTURE);if (cameraIntent.resolveActivity(this.getPackageManager()) != null) {
tempFile= new File(FileUtils.createRootPath(this) + "/" + System.currentTimeMillis() + ".jpg");
LogUtils.e(tempFile.getAbsolutePath());
FileUtils.createFile(tempFile);
Uri uri= FileProvider.getUriForFile(this,
FileUtils.getApplicationId(this) + ".provider", tempFile);
List resInfoList = this.getPackageManager()
.queryIntentActivities(cameraIntent, PackageManager.MATCH_DEFAULT_ONLY);for(ResolveInfo resolveInfo : resInfoList) {
String packageName=resolveInfo.activityInfo.packageName;this.grantUriPermission(packageName, uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION|Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);//Uri.fromFile(tempFile)
startActivityForResult(cameraIntent, REQUEST_CAMERA);
}else{
Toast.makeText(this, "打开相机失败", Toast.LENGTH_SHORT).show();
}
}
@Overrideprotected void onActivityResult(int requestCode, intresultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);if (requestCode == REQUEST_CAMERA && resultCode ==RESULT_OK){if (tempFile != null){
mImageList.add(tempFile.getAbsolutePath());
}
}
}