Android相机应用在KitKat中崩溃

时间:2015-02-17 01:48:04

标签: java android eclipse

我正在构建一个相机应用程序。它在我的Sony Experia手机上正常运行。但是当我要拍照时,它会在KitKat上崩溃。请给我一些帮助。

这是我的相机预览类............

package com.hnjcreations.cameratest;

import java.io.IOException;

import android.annotation.SuppressLint;
import android.content.Context;
import android.hardware.Camera;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

@SuppressLint("ViewConstructor") public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {

    private SurfaceHolder mHolder;
    private Camera mCamera;

    @SuppressWarnings("deprecation")
    public CameraPreview(Context context,Camera camera) {
        super(context);
        mCamera = camera;
        mHolder = getHolder();
        mHolder.addCallback(this);
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    @Override
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {


    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {

        try {
            mCamera.setPreviewDisplay(holder);
            mCamera.startPreview();
            LeafReader.safeToTakePicture=true;
            ColorBarReader.safeToTakePic=true;
            }catch (IOException e) {
            Log.d(" ", "Error setting camera preview: " + e.getMessage());
        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder arg0) {
        if (mHolder.getSurface() == null){
          return;
        }
        try {
            mCamera.stopPreview();
        } catch (Exception e){

        }
        try {
            mCamera.setPreviewDisplay(mHolder);
            mCamera.startPreview();

        } catch (Exception e){

        }
      }

}

这是我的照片上课.........

package com.hnjcreations.cameratest;

import android.os.Build;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Typeface;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class LeafReader extends Activity implements OnClickListener {

    private static Camera mCamera;
    private CameraPreview mPreview;
    ImageView square;
    Button menuButton;
    TextView tv;
    static final int REQUEST_IMAGE_CAPTURE = 1;
    public static final int MEDIA_TYPE_IMAGE = 1;
    int x = 0, y = 0;
    public int reqcolor = 0;
    public static boolean safeToTakePicture = false;
    public static Activity lRead=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.leaf_reader);
        lRead=this;
        startCamera();
        setViews();
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

    @Override
    protected void onPause() 
    {
        super.onPause();
        try
        {    
             mCamera.stopPreview(); 
             mPreview.getHolder().removeCallback(mPreview);
             mCamera.release();
             mCamera = null;
        }
        catch(Exception e)
        {

        }
    }

    @Override
    protected void onResume() {
         super.onResume();
        if(mCamera==null)
         {
           mPreview.getHolder().removeCallback(mPreview);
           mCamera = getCameraInstance();
           startPreview();
           Toast.makeText(LeafReader.this,"startPreview",Toast.LENGTH_SHORT).show();
          }
    }

    // set views
    void setViews() {

        menuButton = (Button) findViewById(R.id.menubutton);
        menuButton.setOnClickListener(this);
        tv=(TextView)findViewById(R.id.sinLeafCap);
        Typeface tf=Typeface.createFromAsset(getAssets(), "KANDYN~1.TTF");
        tv.setTextColor(Color.BLACK);
        tv.setTypeface(tf);
    }

    //
    private static Camera getCameraInstance(){
        Camera c = null;
        try {
            c = Camera.open();
            c.setDisplayOrientation(90);
        } catch (RuntimeException e) {
        }
        return c;
    }

    // start camera
    public void startCamera() {
        mCamera=getCameraInstance();
        startPreview();
    }

    // start preview
    public void startPreview() 
    {
        mPreview = new CameraPreview(this, mCamera);
        FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
        preview.addView(mPreview);
    }

    // menu
    @SuppressLint("NewApi")
    @Override
    public void onClick(View v) {
        switch (v.getId())
             {
                 case R.id.menubutton:
                     if(Mainmenudisp.mainMenu!=null)
                     {
                         Mainmenudisp.mainMenu.finish();
                     }
                     startActivity(new Intent("com.hnjcreations.cameratest.MAINMENU"));
              }
        }

    // take picture
    private PictureCallback mpicture = new PictureCallback() {

        @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
        public void onPictureTaken(byte[] data, Camera camera) {
            try {
                Bitmap mybitmap = getBitmap(data);
                int width = mybitmap.getWidth(), height = mybitmap.getHeight();
                String hexColStr=Integer.toHexString(mybitmap.getPixel(width / 2, height / 2));

                String reqFeriAmount=getFerAmou(hexColStr);

                Intent colorint = new Intent(getBaseContext(), LeafData.class);
                colorint.putExtra("enter code",mybitmap.getPixel(width / 2, height / 2));
                colorint.putExtra("receive code",reqFeriAmount);
                finish();
                startActivity(colorint);

            } catch (Exception e) {
                Toast.makeText(LeafReader.this,"Error",Toast.LENGTH_LONG).show();
            }
        }};



    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (safeToTakePicture) {
            mCamera.takePicture(null, null, mpicture);
        }
        return true;
    }

    // converting byte to bitmap
    public Bitmap getBitmap(byte[] bitmap) {
        return BitmapFactory.decodeByteArray(bitmap, 0, bitmap.length);
    }
}

logcat的:

02-17 07:44:53.781: E/AndroidRuntime(10272): FATAL EXCEPTION: main
02-17 07:44:53.781: E/AndroidRuntime(10272): Process: com.hnjcreations.cameratest, PID: 10272
02-17 07:44:53.781: E/AndroidRuntime(10272): java.lang.RuntimeException: takePicture failed
02-17 07:44:53.781: E/AndroidRuntime(10272):    at android.hardware.Camera.native_takePicture(Native Method)
02-17 07:44:53.781: E/AndroidRuntime(10272):    at android.hardware.Camera.takePicture(Camera.java:1597)
02-17 07:44:53.781: E/AndroidRuntime(10272):    at android.hardware.Camera.takePicture(Camera.java:1542)
02-17 07:44:53.781: E/AndroidRuntime(10272):    at com.hnjcreations.cameratest.ColorBarReader.onTouchEvent(ColorBarReader.java:153)
02-17 07:44:53.781: E/AndroidRuntime(10272):    at android.app.Activity.dispatchTouchEvent(Activity.java:2508)
02-17 07:44:53.781: E/AndroidRuntime(10272):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2093)
02-17 07:44:53.781: E/AndroidRuntime(10272):    at android.view.View.dispatchPointerEvent(View.java:7973)
02-17 07:44:53.781: E/AndroidRuntime(10272):    at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4384)
02-17 07:44:53.781: E/AndroidRuntime(10272):    at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4255)
02-17 07:44:53.781: E/AndroidRuntime(10272):    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3801)
02-17 07:44:53.781: E/AndroidRuntime(10272):    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3851)
02-17 07:44:53.781: E/AndroidRuntime(10272):    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3820)
02-17 07:44:53.781: E/AndroidRuntime(10272):    at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3927)
02-17 07:44:53.781: E/AndroidRuntime(10272):    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3828)
02-17 07:44:53.781: E/AndroidRuntime(10272):    at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3984)
02-17 07:44:53.781: E/AndroidRuntime(10272):    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3801)
02-17 07:44:53.781: E/AndroidRuntime(10272):    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3851)
02-17 07:44:53.781: E/AndroidRuntime(10272):    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3820)
02-17 07:44:53.781: E/AndroidRuntime(10272):    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3828)
02-17 07:44:53.781: E/AndroidRuntime(10272):    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3801)
02-17 07:44:53.781: E/AndroidRuntime(10272):    at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6116)
02-17 07:44:53.781: E/AndroidRuntime(10272):    at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6096)
02-17 07:44:53.781: E/AndroidRuntime(10272):    at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6050)
02-17 07:44:53.781: E/AndroidRuntime(10272):    at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6246)
02-17 07:44:53.781: E/AndroidRuntime(10272):    at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
02-17 07:44:53.781: E/AndroidRuntime(10272):    at android.os.MessageQueue.nativePollOnce(Native Method)
02-17 07:44:53.781: E/AndroidRuntime(10272):    at android.os.MessageQueue.next(MessageQueue.java:138)
02-17 07:44:53.781: E/AndroidRuntime(10272):    at android.os.Looper.loop(Looper.java:150)
02-17 07:44:53.781: E/AndroidRuntime(10272):    at android.app.ActivityThread.main(ActivityThread.java:5333)
02-17 07:44:53.781: E/AndroidRuntime(10272):    at java.lang.reflect.Method.invokeNative(Native Method)
02-17 07:44:53.781: E/AndroidRuntime(10272):    at java.lang.reflect.Method.invoke(Method.java:515)
02-17 07:44:53.781: E/AndroidRuntime(10272):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
02-17 07:44:53.781: E/AndroidRuntime(10272):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
02-17 07:44:53.781: E/AndroidRuntime(10272):    at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案