0

我正在尝试让我的应用程序拍照并返回该照片以供使用。但是,它在模拟器和 Nexus One 上都引发了异常。

这是我的代码:

private File temporaryCameraFile = new File("/sdcard/tmp.bmp");

从菜单中选择拍照时:

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(temporaryCameraFile));
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);

在 onActivityResult()

if(resultCode == RESULT_OK){
    Bitmap cameraPicture = decodeFile(temporaryCameraFile);

    // resize to fit screen and add to queue to be drawn
    if (cameraPicture != null)
        if ((cameraPicture.getWidth() > 0) && (cameraPicture.getHeight() > 0))
            page.SetBackground(ResizeImageToFit(cameraPicture));
}

解码文件()

private Bitmap decodeFile(File f){
    try {
        //decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f),null,o);

        //decode with inSampleSize
        o.inJustDecodeBounds = false;
        Bitmap retval = BitmapFactory.decodeStream(new FileInputStream(f), null, o);

        return retval;
    } catch (FileNotFoundException e) {
        Log.e("decodeFile()", e.toString());
        return null;
    }
}

在 decodeFile() 中,第一个解码正确地返回边界。但是,当我第二次调用它时,模拟器和 Nexus One 都出现以下错误。我尝试更新 decodeFile 以仅在没有 inJustDecodeBounds 方法的情况下进行主解码,但这也失败了。此外,我已手动将文件从设备中拉出,它是有效的位图。

  09-20 15:30:58.711: ERROR/AndroidRuntime(332): Caused by: java.lang.IllegalArgumentException: width and height must be > 0

任何帮助,将不胜感激。

谢谢。

4

1 回答 1

0

请改用内容解析器来解码位图。参考Android相机意图的答案

于 2011-10-01T05:57:07.333 回答