[英]Application crashes when taking photos successively in portrait then in landscape mode
I have a feature in my android application which allow the user to take photos and then save them in a little gallery inside the app. 我的Android应用程序中有一个功能,该功能允许用户拍照,然后将其保存在应用程序内的小画廊中。
Everything works fine when i start taking photo in portrait mode and then stick to portrait mode, but when i decide to switch to landscape mode, i can take the photo but when i click on the confirmation button (the moment when you decide to keep continuing with the actual photo or go back to take another one) the application then crashes. 当我开始以人像模式拍摄照片然后坚持使用人像模式时,一切正常,但是当我决定切换到风景模式时,我可以拍摄照片,但是当我单击确认按钮时(决定继续拍摄的那一刻)使用实际照片或返回拍摄另一张照片),然后应用程序崩溃。 Unfortunately i cannot post the Logs right now, but here is the code (which is fully based on the code you can find on android website) : 不幸的是,我现在无法发布日志,但这是代码(完全基于您可以在android网站上找到的代码):
This is where i create camera intent, the file and save it : 这是我创建相机意图,文件并保存的地方:
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 ;
}
}
And this the the onActivityResult() method : 这是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 ;
}
}
Here is where i put the image into the gallery : 这是我将图像放入图库的位置:
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);
}
I also precise that i call all of these methods into a fragment 我还精确地说,我将所有这些方法称为一个片段
Thank you for reading ! 谢谢您的阅读!
Try something like this: Pass the specific image filename as a argument to your Intent for capture image as putExtra parameter. 尝试这样的操作:将特定的图像文件名作为参数作为putExtra参数传递给您的捕获图像的Intent。 Insert this image Uri in Media Store and now you can use this Uri for your other things. 将此图像Uri插入Media Store,现在您可以将此Uri用于其他用途。 You can check whether image is captured or not by File.exist() 您可以通过File.exist()检查图像是否被捕获
Ex: 例如:
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "Image File name");
Uri mCapturedImageURI = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent intentPicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intentPicture.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
startActivityForResult(intentPicture,ACTION_TAKE_PICTURE);
in onActivityResult() add this: 在onActivityResult()中添加以下内容:
selectedImagePath = getRealPathFromURI(mCapturedImageURI);
Create another method getRealPathFromURI() 创建另一个方法getRealPathFromURI()
//----------------------------------------
/**
* This method is used to get real path of file from from uri
*
* @param contentUri
* @return String
*/
//----------------------------------------
public String getRealPathFromURI(Uri contentUri)
{
try
{
String[] proj = {MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
catch (Exception e)
{
return contentUri.getPath();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.