我的 android 应用程序中有一个功能,它允许用户拍照,然后将它们保存在应用程序内的一个小画廊中。
当我开始以纵向模式拍照然后坚持纵向模式时一切正常,但是当我决定切换到横向模式时,我可以拍照但是当我单击确认按钮时(您决定继续继续的那一刻使用实际照片或返回拍摄另一张)应用程序然后崩溃。不幸的是,我现在无法发布日志,但这里是代码(完全基于您可以在 android 网站上找到的代码):
这是我创建相机意图,文件并保存它的地方:
private File createImageFile() throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "UNICAR_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, //-/ prefix
".jpg", //-/ suffix
storageDir //-/ directory
);
SaisieMission activity = (SaisieMission) getActivity();
activity.setCurrentPhotoPath("file:" + image.getAbsolutePath());
return image;
}
protected void dispatchTakePictureIntent() {
Context context = getActivity();
PackageManager packageManager = context.getPackageManager();
if (((SaisieMission) getActivity()).getNoPath() < 10) {
photoPaths = mission.getPaths();
if(packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA) == false) {
Toast.makeText(getActivity(), "xxx", Toast.LENGTH_SHORT)
.show();
return ;
}
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
SaisieMission activity = (SaisieMission) getActivity();
if (takePictureIntent.resolveActivity(context.getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
Toast toast = Toast.makeText(context, "xxx", Toast.LENGTH_SHORT);
toast.show();
}
if (photoFile != null) {
Uri fileUri = Uri.fromFile(photoFile);
activity.setCapturedImageURI(fileUri);
activity.setCurrentPhotoPath(fileUri.getPath());
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
activity.getCapturedImageURI());
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
} else {
Toast.makeText(getActivity(), getString(R.string.enough_photo), Toast.LENGTH_SHORT)
.show();
return ;
}
}
这是 onActivityResult() 方法:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_TAKE_PHOTO && resultCode == Activity.RESULT_OK) {
SaisieMission saisiemission = (SaisieMission) getActivity();
galleryAddPic();
photoPaths[saisiemission.getNoPath()] = saisiemission.getCurrentPhotoPath();
saisiemission.incNoPath();
mission.setPaths(photoPaths);
} else {
Toast.makeText(getActivity(), "xxx", Toast.LENGTH_SHORT)
.show();
return ;
}
}
这是我将图像放入画廊的地方:
private void galleryAddPic() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
SaisieMission activity = (SaisieMission) getActivity();
File f = new File(activity.getCurrentPhotoPath());
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.getActivity().sendBroadcast(mediaScanIntent);
}
我还准确地说我将所有这些方法都称为一个片段
感谢您的阅读!