应用程序崩溃而试图采取新的照片 [英] App crashing while trying to take new photo

查看:108
本文介绍了应用程序崩溃而试图采取新的照片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个定制的相机应用。当应用程序启动相机打开有在底部的三个按钮。

I am developing a customized camera app. when app starts camera opens there are three buttons at bottom.


  1. 拍摄(拍摄图片)。

  2. 采取新的(从preVIEW相机再次拍摄照片的回报。)

  3. 不使用任何东西。

一切工作正常。 pressing捕捉拍摄照片和previews准确地拍摄的照片。但是,当我preSS采取新的按钮,进入相机模式从preVIEW模式应用程序崩溃不会有任何想法,我做错了。以下是code我使用。

Everything works fine. pressing capture takes the picture and previews the taken picture accurately. But when I press take new button to come into camera mode from preview mode the app crashes don't have any idea what I am doing wrong. following is the code I am using.

public class MainActivity extends Activity {


   protected static final String TAG = null;
   private Camera mCamera;
    private CameraPreview mPreview;
    public static final int MEDIA_TYPE_IMAGE = 1;
    static int result;
    static int degrees = 90;
    private Button captureButton, btn_new;
    public FrameLayout preview;
    private static File mediaFile;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // Create an instance of Camera
    mCamera = getCameraInstance();

    // Create our Preview view and set it as the content of our activity.
    mPreview = new CameraPreview(this, mCamera);
    FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
    preview.addView(mPreview);
     captureButton = (Button) findViewById(R.id.button_capture);
    captureButton.setOnClickListener(
        new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // get an image from the camera
                mCamera.takePicture(null, null, mPicture);
            }
        }
    );
    btn_new = (Button) findViewById(R.id.button_new);
    btn_new.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            mCamera = getCameraInstance();

            //preview = (FrameLayout) findViewById(R.id.camera_preview);
            //preview.addView(mPreview);
        }
    });

}

private PictureCallback mPicture = new PictureCallback() {

    @Override
    public void onPictureTaken(byte[] data, Camera camera) {

        File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
        if (pictureFile == null){

            return;
        }

        try {
            FileOutputStream fos = new FileOutputStream(pictureFile);
            fos.write(data);
            fos.close();
        } catch (FileNotFoundException e) {
            Log.d(TAG, "File not found: " + e.getMessage());
        } catch (IOException e) {
            Log.d(TAG, "Error accessing file: " + e.getMessage());
        }
    }
};

/** Create a File for saving an image or video */
private static File getOutputMediaFile(int type){
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.

    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "Camera");

    // Create the storage directory if it does not exist
    if (! mediaStorageDir.exists()){
        if (! mediaStorageDir.mkdirs()){
            Log.d("MyCameraApp", "failed to create directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());

    if (type == MEDIA_TYPE_IMAGE){
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
        "IMG_"+ timeStamp + ".jpg");
    } 
    else {
        return null;
    }

    return mediaFile;
}
@SuppressWarnings("null")
public static Camera getCameraInstance(){
    Camera c = null;
    Context context = null; 
    try{
        c = Camera.open();
        //setCameraDisplayOrientation(MainActivity, 0, c);
        c.setDisplayOrientation(degrees);
    }
    catch (Exception e) {
        Toast.makeText(context.getApplicationContext(),"Camera is not available" ,           Toast.LENGTH_LONG).show();

    }
    return c;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}

在此先感谢。
最后
这里是我的错误logcat的:

Thanks in Advance. Finally here is my error logcat:

18 02-21:24:01.868:E / AndroidRuntime(937):致命异常:主要
02-21 18:24:01.868:E / AndroidRuntime(937):显示java.lang.NullPointerException
02-21 18:24:01.868:E / AndroidRuntime(937):在com.example.facebooktag.MainActivity.getCameraInstance(MainActivity.java:136)
02-21 18:24:01.868:E / AndroidRuntime(937):在com.example.facebooktag.MainActivity $ 3.onClick(MainActivity.java:66)
02-21 18:24:01.868:E / AndroidRuntime(937):在android.view.View.performClick(View.java:4202)
02-21 18:24:01.868:E / AndroidRuntime(937):在android.view.View $ PerformClick.run(View.java:17340)
02-21 18:24:01.868:E / AndroidRuntime(937):在android.os.Handler.handleCallback(Handler.java:725)
02-21 18:24:01.868:E / AndroidRuntime(937):在android.os.Handler.dispatchMessage(Handler.java:92)
02-21 18:24:01.868:E / AndroidRuntime(937):在android.os.Looper.loop(Looper.java:137)
02-21 18:24:01.868:E / AndroidRuntime(937):在android.app.ActivityThread.main(ActivityThread.java:5039)
02-21 18:24:01.868:E / AndroidRuntime(937):在java.lang.reflect.Method.invokeNative(本机方法)
02-21 18:24:01.868:E / AndroidRuntime(937):在java.lang.reflect.Method.invoke(Method.java:511)
02-21 18:24:01.868:E / AndroidRuntime(937):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)
02-21 18:24:01.868:E / AndroidRuntime(937):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-21 18:24:01.868:E / AndroidRuntime(937):在dalvik.system.NativeStart.main(本机方法)

02-21 18:24:01.868: E/AndroidRuntime(937): FATAL EXCEPTION: main 02-21 18:24:01.868: E/AndroidRuntime(937): java.lang.NullPointerException 02-21 18:24:01.868: E/AndroidRuntime(937): at com.example.facebooktag.MainActivity.getCameraInstance(MainActivity.java:136) 02-21 18:24:01.868: E/AndroidRuntime(937): at com.example.facebooktag.MainActivity$3.onClick(MainActivity.java:66) 02-21 18:24:01.868: E/AndroidRuntime(937): at android.view.View.performClick(View.java:4202) 02-21 18:24:01.868: E/AndroidRuntime(937): at android.view.View$PerformClick.run(View.java:17340) 02-21 18:24:01.868: E/AndroidRuntime(937): at android.os.Handler.handleCallback(Handler.java:725) 02-21 18:24:01.868: E/AndroidRuntime(937): at android.os.Handler.dispatchMessage(Handler.java:92) 02-21 18:24:01.868: E/AndroidRuntime(937): at android.os.Looper.loop(Looper.java:137) 02-21 18:24:01.868: E/AndroidRuntime(937): at android.app.ActivityThread.main(ActivityThread.java:5039) 02-21 18:24:01.868: E/AndroidRuntime(937): at java.lang.reflect.Method.invokeNative(Native Method) 02-21 18:24:01.868: E/AndroidRuntime(937): at java.lang.reflect.Method.invoke(Method.java:511) 02-21 18:24:01.868: E/AndroidRuntime(937): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 02-21 18:24:01.868: E/AndroidRuntime(937): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 02-21 18:24:01.868: E/AndroidRuntime(937): at dalvik.system.NativeStart.main(Native Method)

推荐答案

井空指针异常是从你的异常来。你有

Well the null pointer exception is coming from your exception. you have

Context context = null; 

,然后你正试图在这里使用它:

and then you are trying to use it here:

Toast.makeText(context.getApplicationContext(),"Camera is not available" ,           Toast.LENGTH_LONG).show();

没有任何地方实际上可以初始化它。

without acutally initializing it anywhere.

另外,在你的onPause()函数,你应该好自己清理。 (如发布相机,停止表面preVIEW,诸如此类的事情)。

Also in you onPause() function you should clean up after yourself. (eg. release camera, stop surface preview, that sort of thing).

这篇关于应用程序崩溃而试图采取新的照片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆