安卓相机打不开
Posted
技术标签:
【中文标题】安卓相机打不开【英文标题】:AndroidCamera won't open 【发布时间】:2016-07-26 18:05:43 【问题描述】:我关注this guide 以获取我的Cameraapplication
的基础,我已经更改了一些代码以使其正常工作,因为教程中有一些错误和类似的东西,但是背面摄像头带有预览功能,一切正常工作,我可以拍一张照片保存在sd卡上。
现在我的问题是,我希望能够切换到前置摄像头,但我无法让它工作。我已经在网上搜索了几个小时,但我无法让它工作。我收到此错误:
java.lang.RuntimeException: Fail to connect to camera service
这是我尝试切换到相机时的代码:
OnClickListener switchCameraListener = new OnClickListener()
@Override
public void onClick(View v)
findFrontFacingCamera();
chooseCamera();
;
所有其他代码都在我发送的那个链接上。如果这样更容易,我当然可以在这里发布它,所以请告诉我是否应该在这里编写任何其他代码。我非常感谢任何帮助。谢谢! /亚历克斯
编辑:现在在我的两个班级上发布代码:
package com.alexborghesi.androidcameraexample;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.hardware.Camera.PictureCallback;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
public class cameraClass extends Activity implements SensorEventListener
Shake shake = new Shake();
private Camera mCamera;
private previewClass mPreview;
private PictureCallback mPicture;
private Button capture, switchCamera;
private Context myContext;
private RelativeLayout cameraPreview;
private boolean cameraFront = false;
Sensor acc;
SensorManager accManager;
TextView textView;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
myContext = this;
accManager = (SensorManager) getSystemService(SENSOR_SERVICE);
acc = accManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
accManager.registerListener(this, acc, SensorManager.SENSOR_DELAY_UI);
textView = (TextView) findViewById(R.id.textView);
initialize();
public void initialize()
cameraPreview = (RelativeLayout) findViewById(R.id.camera_preview);
mPreview = new previewClass(myContext, mCamera);
cameraPreview.addView(mPreview);
capture = (Button) findViewById(R.id.button_capture);
capture.setOnClickListener(captureListener);
switchCamera = (Button) findViewById(R.id.button_ChangeCamera);
switchCamera.setOnClickListener(switchCameraListener);
private int findFrontFacingCamera()
int cameraId = -1;
// Search for the front facing camera
int numberOfCameras = Camera.getNumberOfCameras();
for (int i = 0; i < numberOfCameras; i++)
CameraInfo info = new CameraInfo();
Camera.getCameraInfo(i, info);
if (info.facing == CameraInfo.CAMERA_FACING_FRONT)
cameraId = i;
cameraFront = true;
break;
return cameraId;
private int findBackFacingCamera()
int cameraId = -1;
//Search for the back facing camera
//get the number of cameras
int numberOfCameras = Camera.getNumberOfCameras();
//for every camera check
for (int i = 0; i < numberOfCameras; i++)
CameraInfo info = new CameraInfo();
Camera.getCameraInfo(i, info);
if (info.facing == CameraInfo.CAMERA_FACING_BACK)
cameraId = i;
cameraFront = false;
break;
return cameraId;
public void onResume()
super.onResume();
if (!hasCamera(myContext))
Toast toast = Toast.makeText(myContext, "Sorry, your phone does not have a camera!", Toast.LENGTH_LONG);
toast.show();
finish();
if (mCamera == null)
if (findFrontFacingCamera() < 0)
Toast.makeText(this, "No front facing camera found.", Toast.LENGTH_LONG).show();
switchCamera.setVisibility(View.GONE);
mCamera = Camera.open(findBackFacingCamera());
mPicture = getPictureCallback();
mPreview.refreshCamera(mCamera);
;
public void chooseCamera()
//if the camera preview is the front
if (cameraFront == false)
int cameraId = findBackFacingCamera();
if (cameraId <= 0)
//open the backFacingCamera
//set a picture callback
//refresh the preview
mCamera = Camera.open(cameraId);
mPicture = getPictureCallback();
mPreview.refreshCamera(mCamera);
else
int cameraId = findFrontFacingCamera();
if (cameraId >= 1)
//open the frontFacingCamera
//set a picture callback
//refresh the preview
mCamera = Camera.open(cameraId);
mPicture = getPictureCallback();
mPreview.refreshCamera(mCamera);
@Override
protected void onPause()
super.onPause();
//mSensorManager.unregisterListener(mSensorListener);
//when on Pause, release camera in order to be used from other applications
releaseCamera();
private boolean hasCamera(Context context)
//check if the device has camera
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA))
return true;
else
return false;
private PictureCallback getPictureCallback()
PictureCallback picture = new PictureCallback()
@Override
public void onPictureTaken(byte[] data, Camera camera)
//make a new picture file
File pictureFile = getOutputMediaFile();
if (pictureFile == null)
return;
try
//write the file
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
Toast toast = Toast.makeText(myContext, "Picture saved: " + pictureFile.getName(), Toast.LENGTH_LONG);
toast.show();
catch (FileNotFoundException e)
catch (IOException e)
//refresh camera to continue preview
mPreview.refreshCamera(mCamera);
;
return picture;
OnClickListener captureListener = new OnClickListener()
@Override
public void onClick(View v)
mCamera.takePicture(null, null, mPicture);
;
OnClickListener switchCameraListener = new OnClickListener()
@Override
public void onClick(View v)
findFrontFacingCamera();
chooseCamera();
;
//make picture and save to a folder
private static File getOutputMediaFile()
//make a new file directory inside the "sdcard" folder
File mediaStorageDir = new File("/sdcard/", "JCG Camera");
//if this "JCGCamera folder does not exist
if (!mediaStorageDir.exists())
//if you cannot make this folder return
if (!mediaStorageDir.mkdirs())
return null;
//take the current timeStamp
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
//and make a media file:
mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
return mediaFile;
private void releaseCamera()
// stop and release camera
if (mCamera != null)
mCamera.release();
mCamera = null;
@Override
public void onSensorChanged(SensorEvent event)
if(shake.thresholdSpan(event, -6.0, 6.0))
textView.setText("I'm Shaking!");
else
textView.setText("SKAKA DÅ!!");
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy)
二等:
package com.alexborghesi.androidcameraexample;
import java.io.IOException;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.hardware.Camera;
import android.util.Log;
import android.view.Display;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;
public class previewClass extends SurfaceView implements SurfaceHolder.Callback
private SurfaceHolder mHolder;
private Camera mCamera;
private Camera.Parameters mParameters;
public previewClass(Context context, Camera camera)
super(context);
mCamera = camera;
mHolder = getHolder();
mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
public void surfaceCreated(SurfaceHolder holder)
try
// create the surface and start camera preview
if (mCamera == null)
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
catch (IOException e)
Log.d(VIEW_LOG_TAG, "Error setting camera preview: " + e.getMessage());
public void refreshCamera(Camera camera)
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
setCamera(camera);
try
mCamera.setPreviewDisplay(mHolder);
mCamera.setDisplayOrientation(90);
mCamera.startPreview();
catch (Exception e)
Log.d(VIEW_LOG_TAG, "Error starting camera preview: " + e.getMessage());
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h)
mCamera.stopPreview();
// If your preview can change or rotate, take care of those events here.
// Make sure to stop the preview before resizing or reformatting it.
refreshCamera(mCamera);
public void setCamera(Camera camera)
//method to set a camera instance
mCamera = camera;
@Override
public void surfaceDestroyed(SurfaceHolder holder)
// TODO Auto-generated method stub
// mCamera.release();
我的设备运行的是 android 4.2.2。
【问题讨论】:
请在此处发布代码。 也许本指南可以帮助您:***.com/questions/23904459/… 代码加了,Kyle我试过了,不行:/ 【参考方案1】:你说 Manifest.xml 中的相机权限?
<manifest
<application
<Uses-permission android: name = "android.permission.CAMERA" />
</ Application>
</ Manifest>
我帮过忙
【讨论】:
是这个错误给了我,如果清单中没有说明..,你使用的是什么版本的android?如果是 Android 6.0,则具有运行时权限。 尝试将相机声明为static private static Camera mCamera; 4.2.2 在我正在使用的设备中。【参考方案2】:自己解决了这个问题,问题是我在尝试使用第二个相机之前没有释放相机。
我刚刚添加了这行代码:
releaseCamera();
这个方法包含这个:
private void releaseCamera()
// stop and release camera
if (mCamera != null)
mCamera.release();
mCamera = null;
然后在我的点击监听器中它看起来像这样:
OnClickListener switchCameraListener = new OnClickListener()
@Override
public void onClick(View v)
releaseCamera();
chooseCamera();
;
【讨论】:
以上是关于安卓相机打不开的主要内容,如果未能解决你的问题,请参考以下文章