我正在开发一个具有从相机或画廊拍摄图像的功能的应用程序。这在所有设备中都可以正常工作,但在 OnePlus 中却不行。
这是代码:
public File dispatchTakePictureIntent(Activity activity) {
File tempPhotoFile = null;
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(activity.getPackageManager()) != null) {
try {
tempPhotoFile = Utils.createImageFile(activity);
} catch (Exception e) {
e.printStackTrace();
}
if (tempPhotoFile != null) {
Uri photoURI = FileProvider.getUriForFile(activity, Utils.getFileProvider(activity), tempPhotoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
takePictureIntent.setClipData(ClipData.newRawUri("", photoURI));
takePictureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
activity.startActivityForResult(takePictureIntent, Constants.REQUEST_IMAGE_CAPTURE);
}
}
return tempPhotoFile;
}
实用程序中的代码:
public static File createImageFile(Context context) throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.ENGLISH).format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
return image;
}
现在该应用程序在其他设备上运行良好。但是在 OnePlus 中,当相机打开并单击图像并显示预览时,当时在 logcat 中,我们看到应用程序显示为死机并重新启动,并在应用程序主屏幕上打开。当我们从打开相机应用程序的位置转到特定选项卡时,我们可以看到状态已保存并进入同一页面。我们如何解决这个问题?