致命异常:java.lang.RuntimeException: takePicture失败



我想在没有预览相机的情况下拍摄一张照片。我参考了许多文章,其中一些是:

http://cell0907.blogspot.in/2014/01/android-camera-capture-without.html

http://android-er.blogspot.com.es/2010/12/implement-takepicture-function-of.html

我已经添加了相机和写入外部存储的权限。

在我的活动的xml中,我使用的是surfaceView组件,其宽度和高度为1dp。还有一个按钮,点击这个按钮,一个图片必须被捕获。下面是我的XML文件的代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".CameraActivity"
    android:background="#000000" >
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="capture" />
    <SurfaceView
        android:id="@+id/surfaceView1"
        android:layout_width="1dp"
        android:layout_height="1dp"
        android:layout_below="@+id/button1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="78dp" />
</RelativeLayout>

参照上面提到的链接,我在相应的Activity文件中执行了以下操作:

package com.samplecamera;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.hardware.Camera;
    import android.hardware.Camera.CameraInfo;
    import android.os.Bundle;
    import android.os.Environment;
    import android.app.Activity;
    import android.text.format.DateFormat;
    import android.view.Menu;
    import android.view.SurfaceView;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    public class CameraActivity extends Activity {
        SurfaceView sv=null;
        Camera cam=null;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_camera);
            cam=Camera.open();
            sv=(SurfaceView) findViewById(R.id.surfaceView1);
            try {  
                cam.setPreviewDisplay(sv.getHolder());  
           } catch (IOException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
           }  
             cam.startPreview();  
             Camera.Parameters params = cam.getParameters();  
             params.setJpegQuality(100);  
             cam.setParameters(params);
             Button btn=(Button) findViewById(R.id.button1);
             btn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                      cam.startPreview();
                      cam.takePicture(null, null, mCall);
                }
            });
        }
        Camera.PictureCallback mCall = new Camera.PictureCallback() {  
            public void onPictureTaken(byte[] data, Camera camera) {  
               //decode the data obtained by the camera into a Bitmap  
                  //display.setImageBitmap(photo);  
                 // Bitmap bitmapPicture  
                //  = BitmapFactory.decodeByteArray(data, 0, data.length);  
                  FileOutputStream fos = null;
                try {
                    fos = new FileOutputStream(Environment.getExternalStorageDirectory()+"/"+
                                DateFormat.format("yyyy-MM-dd_kk-mm-ss", new Date().getTime())+".jpeg");
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                    try {
                        fos.write(data);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    try {
                        fos.flush();
                        fos.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }  
             }        
       }; 
        @Override
        protected void onDestroy() {
            // TODO Auto-generated method stub
            cam.release();
            super.onDestroy();
        }
        @Override
        public void onBackPressed() {
            // TODO Auto-generated method stub
            super.onBackPressed();
        }
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.camera, menu);
            return true;
        }
    }

点击按钮,我得到以下异常:

03-21 11:55:44.404: E/AndroidRuntime(27544): FATAL EXCEPTION: main
03-21 11:55:44.404: E/AndroidRuntime(27544): java.lang.RuntimeException: takePicture failed
03-21 11:55:44.404: E/AndroidRuntime(27544):    at android.hardware.Camera.native_takePicture(Native Method)
03-21 11:55:44.404: E/AndroidRuntime(27544):    at android.hardware.Camera.takePicture(Camera.java:1163)
03-21 11:55:44.404: E/AndroidRuntime(27544):    at android.hardware.Camera.takePicture(Camera.java:1108)
03-21 11:55:44.404: E/AndroidRuntime(27544):    at com.samplecamera.CameraActivity$2.onClick(CameraActivity.java:55)
03-21 11:55:44.404: E/AndroidRuntime(27544):    at android.view.View.performClick(View.java:4084)
03-21 11:55:44.404: E/AndroidRuntime(27544):    at android.view.View$PerformClick.run(View.java:16987)
03-21 11:55:44.404: E/AndroidRuntime(27544):    at android.os.Handler.handleCallback(Handler.java:615)
03-21 11:55:44.404: E/AndroidRuntime(27544):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-21 11:55:44.404: E/AndroidRuntime(27544):    at android.os.Looper.loop(Looper.java:137)
03-21 11:55:44.404: E/AndroidRuntime(27544):    at android.app.ActivityThread.main(ActivityThread.java:4802)
03-21 11:55:44.404: E/AndroidRuntime(27544):    at java.lang.reflect.Method.invokeNative(Native Method)
03-21 11:55:44.404: E/AndroidRuntime(27544):    at java.lang.reflect.Method.invoke(Method.java:511)
03-21 11:55:44.404: E/AndroidRuntime(27544):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
03-21 11:55:44.404: E/AndroidRuntime(27544):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
03-21 11:55:44.404: E/AndroidRuntime(27544):    at dalvik.system.NativeStart.main(Native Method)

同样请帮忙。

从onclick事件中删除cam.startpreview()函数,并在为媒体相关项目编写代码时尝试检查camera==null,然后进一步移动,通过使用此代码,您的代码将不会崩溃

最新更新