给一段《Android开发应用实战详解》上的例子:采用bm.compress()压缩转档实现,但每次都抛出异常,求问怎么回事
- Java code
public void onPictureTaken(byte[] _data, Camera _camera) { //把摄像头传回的data转成Bitmap类对象,然后就已经是bmp图像了? Bitmap bm = BitmapFactory.decodeByteArray(_data, 0 ,_data.length); //创建文件在制定目录,创建sdcard/camera_snap.jpg File myCaptureFile = new File(strCaptureFilePath); resetCamera();//到此都可以执行 try { //定义bos,把bm压缩转档到bos.问题:bos为什么这么nb? BufferedOutputStream bos = new BufferedOutputStream (new FileOutputStream(myCaptureFile)); jumpOutAToast("bos defination done!"); //bm压缩转档到bos bm.compress(Bitmap.CompressFormat.JPEG, 80, bos); jumpOutAToast("bos compress done!"); //更新BufferedOutputStream bos.flush(); bos.close(); jumpOutAToast("bos.close() done"); initCamera(); } catch(Exception e) { jumpOutAToast("bos failed"); e.printStackTrace(); //System.out.println(); Log.e(TAG, e.getMessage() ); }
------解决方案--------------------
把日志贴上来看看
------解决方案--------------------
public void onPictureTaken(byte[] _data, Camera _camera)
{
// TODO Handle JPEG image data
/* onPictureTaken传入的第一个参数即为相片的byte */
Bitmap bm = BitmapFactory.decodeByteArray
(_data, 0, _data.length);
/* 创建新文件 */
File myCaptureFile = new File(strCaptureFilePath);
try
{
BufferedOutputStream bos = new BufferedOutputStream
(new FileOutputStream(myCaptureFile));
/* 采用压缩转档方法 */
bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);
bos.flush();
bos.close();
/* 将拍照下来且存储完毕的图文件,显示出来 */
myImageview.setImageBitmap(bm);
resetCamera();
initCamera();
}
catch (Exception e)
{
Log.e(TAG, e.getMessage());
}