gpt4 book ai didi

android - 在纵向模式和横向模式下连续拍摄照片时应用程序崩溃

转载 作者:行者123 更新时间:2023-11-30 02:40:17 24 4
gpt4 key购买 nike

我的 android 应用程序中有一个功能,允许用户拍照,然后将它们保存在应用程序内的一个小图库中。
当我开始以纵向模式拍照然后坚持纵向模式时一切正常,但是当我决定切换到横向模式时,我可以拍照但是当我点击确认按钮时(你决定继续的那一刻使用实际照片或返回拍摄另一张照片)然后应用程序崩溃。不幸的是,我现在无法发布日志,但这是代码(完全基于您可以在 android 网站上找到的代码):

这是我创建相机 Intent 、文件并保存它的地方:

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);
}

我还准确地说,我将所有这些方法调用到一个 fragment 中

感谢阅读!

最佳答案

尝试这样的事情:将特定图像文件名作为参数传递给您的 Intent for capture image 作为 putExtra 参数。将此图像 Uri 插入 Media Store,现在您可以将此 Uri 用于其他用途。您可以通过 File.exist() 检查图像是否被捕获

例如:

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);

onActivityResult() 中添加:

selectedImagePath = getRealPathFromURI(mCapturedImageURI);

创建另一个方法 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();
}
}

关于android - 在纵向模式和横向模式下连续拍摄照片时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25863082/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com