1

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

我还准确地说我将所有这些方法都称为一个片段

感谢您的阅读!

4

1 回答 1

1

尝试这样的事情:将特定图像文件名作为参数传递给您的 Intent 以作为 putExtra 参数捕获图像。将此图像 Uri 插入媒体商店,现在您可以将此 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();
        }
    }
于 2014-09-16T10:11:10.247 回答