Camera拍照报Camera Server died错误

郝炯淞 发布于 2012/07/27 13:43
阅读 5K+
收藏 0

写的一个小测试程序,点击拍照之后,屏幕就暂停了,back之后看了下log说

Camera server died!

ICamera died

Error 100

网上找了很多资料,都没有答案,要么就是答案没贴出来,望大神们支招了。

部分代码如下:

 

public class SnapShot extends Activity implements OnClickListener,SurfaceHolder.Callback,Camera.PictureCallback,ViewSwitcher.ViewFactory {

	private SurfaceView cameraView = null;
	private SurfaceHolder surfaceHolder = null;
	private Camera camera = null;
	private TextSwitcher timerNumber = null;
	private Button timerBtn = null;
	private boolean isRunning = false;
	private int currentTime = 0;
	private static final int settingTime = 10;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.surface);
		
		currentTime = settingTime;
		timerNumber = (TextSwitcher) findViewById(R.id.timerText);
		timerBtn = (Button) findViewById(R.id.startTimer);
		timerNumber.setFactory(this);
		Animation in = AnimationUtils.loadAnimation(this,android.R.anim.fade_in);
        Animation out = AnimationUtils.loadAnimation(this, android.R.anim.fade_out);
        timerNumber.setInAnimation(in);
        timerNumber.setOutAnimation(out);
		
		cameraView = (SurfaceView) findViewById(R.id.cameraView);
		surfaceHolder = cameraView.getHolder();
		surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
		surfaceHolder.addCallback(this);
		timerBtn.setOnClickListener(this);
		
	}
	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		
	}
	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		camera = Camera.open();
		try{
			Camera.Parameters parameters = camera.getParameters();
			if(this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE){
				//这是一个众所周知但未文档化的特性
				parameters.set("orientation", "portrait");
				camera.setDisplayOrientation(90);
				parameters.setRotation(90);
			}else{
				parameters.set("orientation", "landscape");
				camera.setDisplayOrientation(0);
				parameters.setRotation(0);
			}
			camera.setParameters(parameters);
			camera.setPreviewDisplay(holder);
		}catch(Exception ex){
			camera.release();
			ex.printStackTrace();
		}
		camera.startPreview();
	}
	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		camera.setPreviewCallback(null);
		camera.stopPreview();
		camera.release();
		camera = null;
	}
	@Override
	public void onPictureTaken(byte[] data, Camera camera) {
		Uri imageFileUri = this.getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, new ContentValues());
		OutputStream os = null;
		try{
			os = this.getContentResolver().openOutputStream(imageFileUri);
			os.write(data);
			os.flush();
			os.close();
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			try {
				os.flush();
				os.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	private Handler handler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			isRunning = false;
			//拍照
			camera.takePicture(null, null, null,SnapShot.this);
			timerNumber.setVisibility(View.GONE);
		}
	};
	@Override
	public void onClick(View v) {
		timerNumber.setVisibility(View.VISIBLE);
		timerNumber.setText(""+currentTime);
		handler.postDelayed(TimerThread, 1000);
	}
	
	private Thread TimerThread = new Thread(){
		public void run(){
			if(!isRunning){
				isRunning = true;
				currentTime--;
				handler.postDelayed(TimerThread, 1000);
			}else{
				currentTime--;
				if(currentTime >= 1){
					handler.postDelayed(TimerThread, 1000);
				}else{
					handler.sendEmptyMessageDelayed(0, 200);
				}
			}
			timerNumber.setText(""+currentTime);
		}
	};
	@Override
	public View makeView() {
		TextView t = new TextView(this);
		t.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
		t.setTextSize(70);
		return t;
	}
}
//添加的权限:
<uses-permission android:name="android.permission.CAMERA"/>

加载中
返回顶部
顶部