带有ACTION_IMAGE_CAPTURE的decodeBitmap的Android问题

时间:2011-09-20 15:47:05

标签: android android-emulator bitmap android-camera android-camera-intent

我正在尝试让我的应用拍照并返回该图片以供使用。但是,它在模拟器和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));
}

decodeFile()

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

任何帮助都将不胜感激。

感谢。