Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用camera.release()android释放相机时应用程序没有响应_Android_Android Camera - Fatal编程技术网

使用camera.release()android释放相机时应用程序没有响应

使用camera.release()android释放相机时应用程序没有响应,android,android-camera,Android,Android Camera,在我的应用程序中,我使用服务自动捕获图像 从我的服务中,我开始了拍照活动 摄像机活动 public class CameraActivity extends Activity { private static final String TAG = "CameraDemo"; SharedPreferences pref; CameraPreview preview; Button click; // enter email adress from where the email will b

在我的应用程序中,我使用服务自动捕获图像

从我的服务中,我开始了拍照活动

摄像机活动

public class CameraActivity extends Activity {

private static final String TAG = "CameraDemo";
SharedPreferences pref;

CameraPreview preview;
Button click;
// enter email adress from where the email will be sent
String fromPassWord;
int cameraType;
private static final int FRONT_CAMERA = 0;
private static final int BACK_CAMERA = 1;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.camera);
    cameraType = getIntent().getIntExtra("cameraType",BACK_CAMERA);

    preview = new CameraPreview(this,cameraType);
    ((FrameLayout) findViewById(com.abc.xyz.R.id.preview)).addView(preview);
     handler.sendEmptyMessageDelayed(0, 100);
     Log.d(TAG, "onCreate'd");
}

Handler handler = new Handler() {
    public void handleMessage(android.os.Message msg) {
        try{
            if(preview.camera != null ){ 
                 preview.camera.takePicture(shutterCallback, rawCallback, jpegCallback);    
            }else{
                finish();                   
            }
        }catch (Exception e) {
            // TODO: handle exception
            finish();                   
        }
    };
};

ShutterCallback shutterCallback = new ShutterCallback() {
    public void onShutter() {
        Log.d(TAG, "onShutter'd");
        Log.v(TAG, "onShutter'd");
    }
};

PictureCallback rawCallback = new PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {
        Log.d(TAG, "onPictureTaken - raw");
        Log.v(TAG, "onPictureTaken - raw");
    }
};

PictureCallback jpegCallback = new PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {
        try {
            //my method for storing image
            StoreByteImage(data, 75, "ImageName");
            Log.v("on picture taken", "picture callback");
        } catch (Exception e) {
            Log.d(TAG, "Exception--------------------1");
            e.printStackTrace();
        }
        finish();
    }
};
}

CameraPreview.java

class CameraPreview extends SurfaceView implements SurfaceHolder.Callback{

private static final String TAG = "Preview";
SurfaceHolder mHolder;
public Camera camera;
int cameraType;
Context _context;
private static final int FRONT_CAMERA   = 0;
private static final int BACK_CAMERA    = 1;
//SurfaceHolder controls the surface size and format, edit the pixels in the surface, and monitor changes to the surface
CameraPreview(Context context, int type){
    super(context);
    //Return the SurfaceHolder providing access and control over this SurfaceView's underlying surface
    mHolder = getHolder();
    //Add a callback interface to this holder
    mHolder.addCallback(this);
    //Set the surface type
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    this.cameraType =   type;
    _context    =   context;
}

//Create a surface
public void surfaceCreated(SurfaceHolder sHolder){

    if(cameraType == FRONT_CAMERA){
        try{
            camera  =   openFrontFacingCameraGingerbread();  //method to open camera...
            Log.v("cameraFornt", "value"+camera);
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
    }else{
        //do nothing
    }
    if(camera == null)
        return;

    try{
        camera.setPreviewDisplay(sHolder);

        //Installs a callback to be invoked for every preview frame in addition to displaying them on the screen
        camera.setOneShotPreviewCallback(new PreviewCallback(){

            //Called as preview frames are displayed
            public void onPreviewFrame(byte[] data, Camera arg1){

                CameraPreview.this.invalidate();
            }
        });
    } catch (Exception e){
        e.printStackTrace();
    }
}

//called immediately before a surface is being destroyed
public void surfaceDestroyed(SurfaceHolder sHolder){
    if(camera == null){
        return;
    }
    Log.v("Surface dESTroyed ","Camera Preview");
    camera.release();

    }
    Log.v("Camera",""+camera);
}

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h){
    Log.v("SurfaceChangesCalled", "true");
    if(camera == null){
        return;
    }

}

public void draw(Canvas canvas){
    super.draw(canvas);
    //The Paint class holds the style and color information about how to draw geometries, text and bitmaps
    Paint paint = new Paint(Color.RED);
    Log.d(TAG, "draw");
    Log.v("Draw", "Canvas");
    //Draw the text, with origin at (canvas.getWidth()/2, canvas.getHeight()/2), using the specified paint
    canvas.drawText("Preview", canvas.getWidth() / 2, canvas.getHeight() / 2, paint);
}
}

现在我的问题是,当我的主应用程序打开并且相机活动开始时,我得到了ANR。它在摄像机前冻结。释放()

但当我的主应用程序没有打开时,它就像一个魔咒一样工作


我还尝试了在CameraActivity的onPause()方法中释放相机,但没有成功。

您使用的是哪种设备?我们在Nexus10和Nexus10上也有同样的问题(android的4.3、4.4和5.0版本-在4.2上不会发生)。camera.release呼叫挂起30秒。当前的解决方法是在单独的线程中控制相机,然后让线程挂在背景中。这不是一个令人满意的解决方案,因为当我们等待相机发布时,我们无法使用它。