打开系统相机拍照并保存
2018-09-18 本文已影响0人
我是你森哥哥
public void takePhoto() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM + "/Camera/" + System.currentTimeMillis() + ".jpg");
int currentapiVersion = Build.VERSION.SDK_INT;
if (currentapiVersion < 24) {
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
} else {
ContentValues contentValues = new ContentValues(1);
contentValues.put(MediaStore.Images.Media.DATA, file.getAbsolutePath());
Uri uri = context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
}
} catch (Exception e) {
e.printStackTrace();
}
context.startActivity(intent);
}
打开手机中的相机应用
PackageManager packageManager = getPackageManager();
//查看系统中是否有可用的相机应用
List<ResolveInfo> resolveInfoList = packageManager.queryIntentActivities(new Intent(MediaStore.ACTION_IMAGE_CAPTURE), PackageManager.MATCH_DEFAULT_ONLY);
if (resolveInfoList != null && resolveInfoList.size() > 0) {
ResolveInfo resolveInfo = resolveInfoList.get(0);
String packageName = resolveInfo.activityInfo.packageName;
String name1 = resolveInfo.activityInfo.name;
try {
//打开相机
Intent intent = getPackageManager().getLaunchIntentForPackage(packageName);
if (intent == null) {
intent = new Intent(Intent.ACTION_MAIN);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
intent.setPackage(packageName);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
ComponentName name = new ComponentName(packageName, name1);
intent.setComponent(name);
}
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}