public void startOpenCamera() {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Log.i("cccccccccc","123123"+cameraIntent.resolveActivity(getPackageManager()));
if (cameraIntent.resolveActivity(getPackageManager()) != null) {
String dir = Environment.getExternalStorageDirectory()+"/img";
File dirFile = new File(dir);
if(!dirFile.exists()){
dirFile.mkdirs();
}
startActivityForResult(cameraIntent,200);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if(data!=null){
if(requestCode==200){
final Bitmap bitmap =data.getParcelableExtra("data");
String name = System.currentTimeMillis()+".jpg";
StorageHelper.saveBitmap(this,bitmap,name);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
/** 打开照相机拍照 */
public void openCapture() {
try {
Intent take_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
imagePaths = Environment.getExternalStorageDirectory().getPath()
+ "/temp/" + (System.currentTimeMillis() + ".jpg");
// 必须确保文件夹路径存在,否则拍照后无法完成回调
File vFile = new File(imagePaths);
if (!vFile.exists()) {
File vDirPath = vFile.getParentFile();
vDirPath.mkdirs();
} else {
if (vFile.exists()) {
vFile.delete();
}
}
cameraUri = getUriForFile(mContext.get(), mContext.get().getPackageName(), vFile);
take_intent.putExtra(MediaStore.EXTRA_OUTPUT, cameraUri);
take_intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
List resInfoList = mContext.get().getPackageManager()
.queryIntentActivities(take_intent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
mContext.get().grantUriPermission(packageName, cameraUri,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
}
mContext.get().startActivityForResult(take_intent, ACTION_IMAGE_CAPTURE);
} catch (Exception e) {
e.printStackTrace();
}
}
/** 打开照相机录像 */
public void openCamera() {
try {
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
// intent.setAction("android.media.action.VIDEO_CAPTURE");
intent.addCategory("android.intent.category.DEFAULT");
// 保存录像到指定的路径
imagePaths = Environment.getExternalStorageDirectory().getPath()
+ "/temp/" + (System.currentTimeMillis() + ".mp4");
// 必须确保文件夹路径存在,否则拍照后无法完成回调
File vFile = new File(imagePaths);
if (!vFile.exists()) {
File vDirPath = vFile.getParentFile();
vDirPath.mkdirs();
} else {
if (vFile.exists()) {
vFile.delete();
}
}
cameraUri = getUriForFile(mContext.get(), mContext.get().getPackageName(), vFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, cameraUri);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
List resInfoList = mContext.get().getPackageManager()
.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
mContext.get().grantUriPermission(packageName, cameraUri,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
}
mContext.get().startActivityForResult(intent, ACTION_VIDEO_CAPTURE);
} catch (Exception e) {
e.printStackTrace();
}
}
private static Uri getUriForFile(Context context, String name, File vFile) {
Uri cameraUri;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
cameraUri = Uri.fromFile(vFile);
} else {
cameraUri = FileProvider.getUriForFile(context, name, vFile);
}
return cameraUri;
}
保存bitmap代码
public static Uri saveBitmap(Context context, Bitmap bitmap, String bitmapName) {
String fileName;
File file;
if (Build.BRAND.equals("Xiaomi")) {
fileName = Environment.getExternalStorageDirectory().getPath() + "/DCIM/Camera/" + bitmapName;
} else {
fileName = Environment.getExternalStorageDirectory().getPath() + "/DCIM/" + bitmapName;
}
Log.d("path", fileName);
file = new File(fileName);
if (file.exists()) {
file.delete();
}
FileOutputStream outputStream;
try {
outputStream = new FileOutputStream(file);
if (bitmap.compress(Bitmap.CompressFormat.JPEG, 90, outputStream)) {
outputStream.flush();
outputStream.close();
Log.d("path", file.getAbsolutePath());
MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), bitmapName, null);
}
} catch (IOException e) {
e.printStackTrace();
}
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + fileName)));
return Uri.fromFile(file);
}
}