Android 中 ACTION_IMAGE_CAPTURE 的解码位图问题

发布于 2024-12-05 16:37:12 字数 1764 浏览 0 评论 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));
}

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 上都收到以下错误。我尝试更新解码文件以仅执行主解码而不使用 inJustDecodeBounds 方法,但这也失败了。另外,我已手动将文件从设备中拉出,它是有效的位图。

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

任何帮助将不胜感激。

谢谢。

I am trying to have my app take a picture and return that picture for use. However, it is throwing an exception both in the emulator and on a Nexus One.

Here is my code:

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

When chose from the menu to take a picture:

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

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

In decodeFile(), the first decode properly returns the bounds. However, when I call it the second time, I get the following error on both the emulator and the Nexus One. I tried updating the decodeFile to only do the main decode without the inJustDecodeBounds method, but that failed as well. Also, I have pulled the file off of the device manually and it is a valid bitmap.

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

Any help would be appreciated.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谎言 2024-12-12 16:37:12

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

Use the content resolver to decode the bitmap instead. Refer to the answer at Android camera intent

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文