拍照后相机应用程序卡住,不 return 我的应用程序
camera application gets stuck after taking pic and do not return to my application
我正在使用 nexus 5 进行测试。
有时我的相机应用程序在拍照后得到 stuck/hang,但我的应用程序没有 return。它 return 几秒钟后 activityResult() 没有被调用。在三星galaxy j5中点击图片后有时会出现白屏。
我的代码
public void openCamera() {
if (!checkPermission(Manifest.permission.CAMERA, context)) {
requestPermission(Manifest.permission.CAMERA, 11, getActivity());
return;
}
if (!checkPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, context)) {
requestPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, 121, getActivity());
return;
}
if (!checkPermission(Manifest.permission.READ_EXTERNAL_STORAGE, context)) {
requestPermission(Manifest.permission.READ_EXTERNAL_STORAGE, 121, getActivity());
return;
}
boolean flag = true;
try {
photoFile = createImageFile();
} catch (Exception ex) {
flag = false;
Toast.makeText(context, ""+ex, Toast.LENGTH_LONG).show();
UtilityMethods.saveException(ex.getMessage(),"ab");
}
if (photoFile != null&&flag) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if(Build.VERSION.SDK_INT>=24) {
intent.putExtra(MediaStore.EXTRA_OUTPUT, FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".provider", photoFile));
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
}else {
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
}
intent.putExtra("android.intent.extras.CAMERA_FACING", 1);
}else{
UtilityMethods.saveException("file exception"+photoFile,"abc");
}
}
另一种方法
private File createImageFile() throws Exception {
String imageFileName = "JPEG_img";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
if (!storageDir.exists()) {
if (!storageDir.mkdirs()) {
return null;
}
}
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
pathOfPic = image.getAbsolutePath();
return image;
}
使用文件已在 API 24 中更改。
这是适合您的解决方案:
我正在使用 nexus 5 进行测试。 有时我的相机应用程序在拍照后得到 stuck/hang,但我的应用程序没有 return。它 return 几秒钟后 activityResult() 没有被调用。在三星galaxy j5中点击图片后有时会出现白屏。
我的代码
public void openCamera() {
if (!checkPermission(Manifest.permission.CAMERA, context)) {
requestPermission(Manifest.permission.CAMERA, 11, getActivity());
return;
}
if (!checkPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, context)) {
requestPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, 121, getActivity());
return;
}
if (!checkPermission(Manifest.permission.READ_EXTERNAL_STORAGE, context)) {
requestPermission(Manifest.permission.READ_EXTERNAL_STORAGE, 121, getActivity());
return;
}
boolean flag = true;
try {
photoFile = createImageFile();
} catch (Exception ex) {
flag = false;
Toast.makeText(context, ""+ex, Toast.LENGTH_LONG).show();
UtilityMethods.saveException(ex.getMessage(),"ab");
}
if (photoFile != null&&flag) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if(Build.VERSION.SDK_INT>=24) {
intent.putExtra(MediaStore.EXTRA_OUTPUT, FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".provider", photoFile));
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
}else {
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
}
intent.putExtra("android.intent.extras.CAMERA_FACING", 1);
}else{
UtilityMethods.saveException("file exception"+photoFile,"abc");
}
}
另一种方法
private File createImageFile() throws Exception {
String imageFileName = "JPEG_img";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
if (!storageDir.exists()) {
if (!storageDir.mkdirs()) {
return null;
}
}
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
pathOfPic = image.getAbsolutePath();
return image;
}
使用文件已在 API 24 中更改。 这是适合您的解决方案: