在三星 A14 5G (android 14) 相机应用程序中突然调用调用片段和活动的销毁方法。
我已经在清单中添加了配置更改代码。
android:configChanges =“方向|键盘隐藏|屏幕大小”
启动相机的代码:
public static File openCamera(Context mContext, Fragment currentFragment) {
if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "opening camera");
String imageName = dateToString(new Date(), PhotoConstant.IMAGE_NAME_FORMAT);
File filePath = new File(mContext.getExternalFilesDir(""), PhotoConstant.IMAGE_DIR);
//File filePath = new File(PhotoConstant.IMAGES_DEFAULT_PATH, PhotoConstant.IMAGE_DIR);
if (!filePath.exists()) {
Log.e(TAG, "folder not exist, going to create new folder");
filePath.mkdir();
}
File destinationFile = new File(
mContext.getExternalFilesDir(""),
imageName + PhotoConstant.IMAGE_FORMAT);
// File destinationFile = new File(PhotoConstant.IMAGES_DEFAULT_PATH + File.separator + PhotoConstant.IMAGE_DIR, imageName + PhotoConstant.IMAGE_FORMAT);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(destinationFile));
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
intent.putExtra(MediaStore.EXTRA_OUTPUT, AppUtil.getFileUri(mContext, destinationFile));
currentFragment.startActivityForResult(intent, PICK_Camera_IMAGE);
Log.d(TAG, "name of image to be capture " + imageName);
return destinationFile;
}else{
Toast.makeText(mContext,"Please Grant Camera Permission in Setting!",Toast.LENGTH_SHORT).show();
return null;
}
}
public static Uri getFileUri(Context mContext, File destination) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return FileProvider.getUriForFile(mContext,
mContext.getApplicationContext()
.getPackageName() + ".provider", destination);
} else {
return Uri.fromFile(destination);
}
}
当您的应用程序的 UI 位于后台时,其进程可以随时终止。这包括您启动另一个应用程序(例如相机应用程序)并且它进入前台的情况。这是完全正常的,也是 Android 自 2008 年 Android 1.0 以来的工作方式。