我正在使用nexus 5进行测试。 有时我的相机应用程序在拍摄照片后卡住/挂起并且不返回我的应用程序。它会在几秒后返回,并且不会调用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;
}
答案 0 :(得分:1)
在API 24中已经使用了文件。 以下是您的解决方案: