Android 中 ACTION_IMAGE_CAPTURE 的解码位图问题
我试图让我的应用程序拍照并返回该照片以供使用。但是,它在模拟器和 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用内容解析器来解码位图。请参阅 Android 相机意图 中的答案
Use the content resolver to decode the bitmap instead. Refer to the answer at Android camera intent