Android 相机启动预览失败

Posted

技术标签:

【中文标题】Android 相机启动预览失败【英文标题】:Android Camera startPreview failed 【发布时间】:2014-06-13 08:54:12 【问题描述】:

我正在开发一个 android 应用程序,它需要在一半的应用程序屏幕上显示相机视图。我能够在 Android 4.0 设备上成功完成它,但是当我尝试在我的 Nexus One 上运行它时它崩溃了,尽管minSDK 版本已设置为 2.2。这是我的相机预览类。

package com.example.locationtest;
import java.io.IOException;
import android.content.Context;
import android.hardware.Camera;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback 
private static final String TAG = "error";
private SurfaceHolder mHolder;
private Camera mCamera;

public CameraPreview(Context context, Camera camera) 
    super(context);
    mCamera = camera;

    // Install a SurfaceHolder.Callback so we get notified when the
    // underlying surface is created and destroyed.
    mHolder = getHolder();
    mHolder.addCallback(this);



public void surfaceCreated(SurfaceHolder holder) 
    // The Surface has been created, now tell the camera where to draw the preview.
    try 
        mCamera.setPreviewDisplay(holder);
        mCamera.startPreview();
     catch (IOException e) 
       // Log.d(TAG, "Error setting camera preview: " + e.getMessage());
    


public void surfaceDestroyed(SurfaceHolder holder) 
    // empty. Take care of releasing the Camera preview in your activity.
    mCamera.release();


public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) 
    // If your preview can change or rotate, take care of those events here.
    // Make sure to stop the preview before resizing or reformatting it.
    Log.d("Function", "surfaceChanged iniciado");
    if (mHolder.getSurface() == null)
      // preview surface does not exist
      return;
    

    // stop preview before making changes
    try 
        mCamera.stopPreview();
     catch (Exception e)
      // ignore: tried to stop a non-existent preview
    

    // set preview size and make any resize, rotate or
    // reformatting changes here


    // start preview with new settings
    try 
        mCamera.setPreviewDisplay(mHolder);
        mCamera.startPreview();

     catch (Exception e)
        Log.d(TAG, "Error starting camera preview: " + e.getMessage());
    


它给出了以下 logcat:

01-05 18:42:03.540: E/AndroidRuntime(583): FATAL EXCEPTION: main
01-05 18:42:03.540: E/AndroidRuntime(583): java.lang.RuntimeException: startPreview failed
01-05 18:42:03.540: E/AndroidRuntime(583):  at android.hardware.Camera.startPreview(Native     Method)
01-05 18:42:03.540: E/AndroidRuntime(583):  at     com.example.locationtest.CameraPreview.surfaceCreated(CameraPreview.java:31)
01-05 18:42:03.540: E/AndroidRuntime(583):  at android.view.SurfaceView.updateWindow(SurfaceView.java:543)
01-05 18:42:03.540: E/AndroidRuntime(583):  at android.view.SurfaceView.dispatchDraw(SurfaceView.java:348)
01-05 18:42:03.540: E/AndroidRuntime(583):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
01-05 18:42:03.540: E/AndroidRuntime(583):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
01-05 18:42:03.540: E/AndroidRuntime(583):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
01-05 18:42:03.540: E/AndroidRuntime(583):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
01-05 18:42:03.540: E/AndroidRuntime(583):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
01-05 18:42:03.540: E/AndroidRuntime(583):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
01-05 18:42:03.540: E/AndroidRuntime(583):  at android.view.View.draw(View.java:6883)
01-05 18:42:03.540: E/AndroidRuntime(583):  at android.widget.FrameLayout.draw(FrameLayout.java:357)
01-05 18:42:03.540: E/AndroidRuntime(583):  at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
01-05 18:42:03.540: E/AndroidRuntime(583):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
01-05 18:42:03.540: E/AndroidRuntime(583):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
01-05 18:42:03.540: E/AndroidRuntime(583):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
01-05 18:42:03.540: E/AndroidRuntime(583):  at android.view.View.draw(View.java:6883)
01-05 18:42:03.540: E/AndroidRuntime(583):  at android.widget.FrameLayout.draw(FrameLayout.java:357)
01-05 18:42:03.540: E/AndroidRuntime(583):  at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1862)
01-05 18:42:03.540: E/AndroidRuntime(583):  at android.view.ViewRoot.draw(ViewRoot.java:1522)
01-05 18:42:03.540: E/AndroidRuntime(583):  at android.view.ViewRoot.performTraversals(ViewRoot.java:1258)
01-05 18:42:03.540: E/AndroidRuntime(583):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
01-05 18:42:03.540: E/AndroidRuntime(583):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-05 18:42:03.540: E/AndroidRuntime(583):  at android.os.Looper.loop(Looper.java:130)
01-05 18:42:03.540: E/AndroidRuntime(583):  at android.app.ActivityThread.main(ActivityThread.java:3683)
01-05 18:42:03.540: E/AndroidRuntime(583):  at java.lang.reflect.Method.invokeNative(Native Method)
01-05 18:42:03.540: E/AndroidRuntime(583):  at java.lang.reflect.Method.invoke(Method.java:507)
01-05 18:42:03.540: E/AndroidRuntime(583):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-05 18:42:03.540: E/AndroidRuntime(583):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-05 18:42:03.540: E/AndroidRuntime(583):  at dalvik.system.NativeStart.main(Native Method)

【问题讨论】:

检查默认的相机应用程序。也许它也无法访问硬件。 尝试添加'mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);'持有者的addCallback之后 Android Camera will not work. startPreview fails的可能重复 另一种方法是通过使用布尔标志来避免更频繁地调用startPreview(),如果 takePicture 已经在进行中则跳过调用! 您能否解释一下失败时的调用流程是什么?您进行了哪些逻辑调试来帮助确定其失败的原因?错误是一系列操作的结果,如果不了解这些操作,很难正确修复它。 【参考方案1】:

使用 Thread.Sleep(2000) 添加一个新线程,然后开始预览, 以便相机将拍摄的图像显示 2 秒并重新启动相机。

 Thread thread = new Thread(new Runnable() 
                @Override
                public void run() 
                    try 
                        thread.sleep(2000);
                        mCamera.startPreview();
                    
                    catch (InterruptedException e)
                    

                    
                
            );
            thread.start();

【讨论】:

请在投票前提供原因和解决方案。 这个答案看起来很荒谬,但它是唯一让它起作用的东西。所以这里的相机实现似乎很荒谬:-D Upvoted 我必须对此表示赞同,因为这是唯一有效的方法。谢谢@AkashBisariya 似乎没有在所有设备中“修复”【参考方案2】:

遇到了同样的问题,请试试这个:

mCamera.stopPreview();
mCamera.startPreview();
mCamera.setPreviewCallback(null);

【讨论】:

以上是关于Android 相机启动预览失败的主要内容,如果未能解决你的问题,请参考以下文章

Android 相机 takePicture() 失败

相机预览在 Android 上首次加载时不显示

Android相机预览设置适配及显示方式

Android相机第二次拍摄失败

相机预览没有重新启动?

OpenCV - 如何在 Android 中设置全屏相机视图?