gpt4 book ai didi

android - 尝试拍摄新照片时应用程序崩溃

转载 作者:行者123 更新时间:2023-12-03 17:39:23 24 4
gpt4 key购买 nike

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

  • 捕获(拍照)。
  • 拍摄新照片(从预览返回相机再次拍照。)
  • 不用于任何事情。

  • 一切正常。按抓图拍照并准确预览所拍照片。但是当我按下新按钮从预览模式进入相机模式时,应用程序崩溃不知道我做错了什么。以下是我正在使用的代码。
    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;
    }

    }

    提前致谢。
    最后
    这是我的错误日志:

    02-21 18:24:01.868: E/AndroidRuntime(937): 致命异常: main
    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): at 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(Native Method)
    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): at dalvik.system.NativeStart.main(Native Method)

    最佳答案

    这可能是因为您第一次成功拍摄了照片并获取了相机对象,然后您又在这里获取了相机对象。

    此处为 setcontentview 下面的第一个实例。

    //创建一个相机实例

    mCamera = getCameraInstance();

    并尝试再次获取相机实例,但它已经获取。
    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);
    }
    });

    在再次获取相机对象之前,您应该释放第一个对象。
    if(mCamera != null)
    mCamera.release();



    确保您在应用程序标记之前在 list 中声明了权限。

    关于android - 尝试拍摄新照片时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15008272/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com