我试图打开前置摄像头,完全没有添加(只是预览),立即拍照并上传到某处。但是当我打电话给“Camera.open();”时,我的应用程序崩溃了。这是我的代码:
CameraActivity.java:
package eu.andret.remotesilentphoto;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.widget.FrameLayout;
public class CameraActivity extends Activity {
private static CameraActivity instance;
private Camera camera;
@Override
protected synchronized void onCreate(Bundle savedInstanceState) {
instance = this;
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_camera);
boolean front = getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT);
new Alert("" + front).show(this);
if (!front) {
finish();
return;
}
try {
camera = Camera.open();
Log.d("AC", camera + "");
FrameLayout f = (FrameLayout)findViewById(R.id.frameLayout1);
f.addView(new CameraPreview(this, camera));
} catch (Exception ex) {
ex.printStackTrace();
stackTrace(ex);
}
}
public static CameraActivity getInstance() {
return instance;
}
public final void stackTrace(Exception e) {
for (StackTraceElement s:e.getStackTrace()) {
if (s.getClassName().startsWith("eu.andret")) {
new Alert("Error: " + e.getClass().getName() + ": " + e.getMessage() + " at " + s.getClassName() + ".java:" + s.getLineNumber() + ".").show(this);
}
}
}
@Override
protected void onPause() {
super.onPause();
camera.release();
}
@Override
protected void onResume() {
super.onResume();
try {
camera = Camera.open();
Log.d("AC", camera + "");
FrameLayout f = (FrameLayout)findViewById(R.id.frameLayout1);
f.addView(new CameraPreview(this, camera));
} catch (Exception ex) {
ex.printStackTrace();
stackTrace(ex);
}
}
}
CameraPreview.java:
package eu.andret.remotesilentphoto;
import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.os.AsyncTask;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private Camera camera;
private SurfaceHolder surfaceHolder;
private PictureCallback camPictureCallback = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
new UploadPhoto().execute(data);
camera.startPreview();
}
};
@SuppressWarnings("deprecation")
public CameraPreview(Context context, Camera camera) {
super(context);
this.camera = camera;
surfaceHolder = getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
try {
camera.setPreviewDisplay(surfaceHolder);
camera.setDisplayOrientation(180);
camera.takePicture(null, null, camPictureCallback);
} catch (IOException ex) {
ex.printStackTrace();
}
camera.startPreview();
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
camera.setPreviewDisplay(surfaceHolder);
camera.setDisplayOrientation(180);
} catch (IOException ex) {
ex.printStackTrace();
}
camera.startPreview();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (camera != null) {
camera.stopPreview();
camera.setPreviewCallback(null);
camera.release();
camera = null;
}
}
public class UploadPhoto extends AsyncTask<byte[], Void, String> {
@Override
protected String doInBackground(byte[]... params) {
try {
HttpPost httpPost = new HttpPost("some link here");
httpPost.setEntity(new ByteArrayEntity(params[0]));
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse httpResponse = httpClient.execute(httpPost);
String result = EntityUtils.toString(httpResponse.getEntity(), HTTP.UTF_8);
new Alert("0" + result).show(CameraActivity.getInstance());
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
new Alert("1" + result).show(CameraActivity.getInstance());
}
}
}
的AndroidManifest.xml:
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.front" />
例外是:
An error occurred while connecting to camera: 0
java.lang.RuntimeException: Fail to connect to camera service
at android.hardware.Camera.native_setup(Native Method)
at android.hardware.Camera.<init>(Camera.java:351)
at android.hardware.Camera.open(Camera.java:325)
at eu.andret.remotesilentphoto.CameraActivity.onResume(CameraActivity.java:62)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1192)
at android.app.Activity.performResume(Activity.java:5310)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2764)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2803)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2238)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
如何摆脱这种异常?我正在通过GenyMotion在Android 4.4.4(API 19)上进行编译。
答案 0 :(得分:1)
问题是您正在创建两个Camera实例(即Camera.open())但在onPause方法中只释放一个。
这样做 -
if(camera != null)
{
camera = Camera.open();
}