我为此长篇文章提前道歉,但有必要展示正在发生的事情。
我一直在研究适用于1.6模拟器的应用程序,但我的G1上却有炸弹。
这是摄影师的活动:
public class Photographer extends Activity {
private SurfaceView preview = null;
private SurfaceHolder previewHolder = null;
private Camera camera = null;
public static final int IMAGE_HEIGHT = 320;
public static final int IMAGE_WIDTH = 480;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.photographer);
preview = (SurfaceView)findViewById(R.id.preview);
previewHolder = preview.getHolder();
previewHolder.addCallback(surfaceCallback);
previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
private void takePicture() {
camera.stopPreview();
camera.takePicture(null, null, photoCallback);
}
SurfaceHolder.Callback surfaceCallback = new SurfaceHolder.Callback() {
public void surfaceCreated(SurfaceHolder holder) {
camera = Camera.open();
try {
camera.setPreviewDisplay(previewHolder);
}
catch (Throwable t) {
Log.e("Photographer", "Exception in setPreviewDisplay()", t);
}
try {
takePicture();
} catch (Throwable t) {
Log.e("Photographer", "Exception in takePicture()", t);
}
}
public void surfaceChanged(SurfaceHolder holder,
int format, int width, int height) {
Camera.Parameters parameters=camera.getParameters();
parameters.setPreviewSize(IMAGE_WIDTH, IMAGE_HEIGHT);
parameters.setPictureFormat(PixelFormat.JPEG);
camera.setParameters(parameters);
camera.startPreview();
}
public void surfaceDestroyed(SurfaceHolder holder) {
camera.stopPreview();
camera.release();
camera = null;
}
};
Camera.PictureCallback photoCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
new SavePhotoTask().execute(data);
camera.startPreview();
}
};
class SavePhotoTask extends AsyncTask<byte[], String, String> {
@Override
protected String doInBackground(byte[]... jpeg) {
File photo = new File(Environment.getExternalStorageDirectory(), "WIIA-new.jpg");
if (photo.exists())
photo.renameTo(new File(Environment.getExternalStorageDirectory(), "WIIA-old.jpg"));
try {
FileOutputStream fos = new FileOutputStream(photo.getPath());
fos.write(jpeg[0]);
fos.close();
Log.e("Photographer", "Picture taken");
// returning result
Intent returnIntent = new Intent();
setResult(RESULT_OK, returnIntent);
finish();
}
catch (java.io.IOException e) {
Log.e("Photographer", "Exception in photoCallback", e);
// returning result
Intent returnIntent = new Intent();
setResult(RESULT_CANCELED, returnIntent);
finish();
}
return(null);
}
}
}
以下是调用此活动的地方:
public class WIIA extends Activity {
public class CountDown extends CountDownTimer {
private TextView tvCountDown = (TextView) findViewById(R.id.progress);
private TextView tvSystemState = (TextView) findViewById(R.id.system_state);
public CountDown(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
tvSystemState.setText(R.string.system_state_armed);
tvCountDown.setText(R.string.armed);
snapshotInterval = Long.parseLong(settings.getString("snapshot_interval_list", "5"));
imgCaptureTimer = new Timer("imageCapture");
imgCaptureTimer.scheduleAtFixedRate(doRefresh, 0, snapshotInterval*1000);
}
@Override
public void onTick(long millisUntilFinished) {
tvCountDown.setText(millisUntilFinished/1000 + " seconds until Armed");
}
}
private TimerTask doRefresh = new TimerTask() {
public void run() {
if (armed) {
refreshImage();
} else {
imgCaptureTimer.cancel();
}
}
};
private void refreshImage() {
Thread updateThread = new Thread(null, backgroundCaptureImage, "capture_image");
updateThread.start();
}
private Runnable backgroundCaptureImage = new Runnable() {
public void run() {
doRefreshImage();
}
};
private void doRefreshImage() {
Intent photographer = new Intent(this, Photographer.class);
startActivityForResult(photographer, GET_PHOTO);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case (GET_PHOTO) : {
if(resultCode == RESULT_OK) {
// go on to compare images
new ProcessImageTask().execute();
}
break;
}
}
}
无法粘贴AndroidManifest.xml的相关部分,但我拥有Camera的权限:
<uses-permission android:name="android.permission.CAMERA" />
这是摄影师的一部分:
<activity android:name=".Photographer"
android:configChanges="keyboardHidden|orientation"
android:screenOrientation="landscape"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
</activity>
还设置了对G1的测试:
android:debuggable="true"
并使用1.6:
<uses-sdk android:minSdkVersion="4" />
所有这些都适用于1.6模拟器,但当我在G1上运行时,LogCat显示:
INFO/ActivityManager(74): Starting activity: Intent { cmp=com.androidsecurity.wiia/.Photographer }
WARN/WindowManager(74): App freeze timeout expired.
WARN/WindowManager(74): Force clearing freeze: AppWindowToken{432e1520 token=HistoryRecord{43124c10 com.androidsecurity.wiia/.Photographer}}
WARN/ActivityManager(74): Activity pause timeout for HistoryRecord{43124c10 com.androidsecurity.wiia/.Photographer}
ERROR/QualcommCameraHardware(51): native_get_picture: MSM_CAM_IOCTL_GET_PICTURE fd 13 error Connection timed out
ERROR/QualcommCameraHardware(51): getPicture failed!
DEBUG/QualcommCameraHardware(51): snapshot_thread X
INFO/QualcommCameraHardware(51): initPreview E: preview size=480x320
DEBUG/QualcommCameraHardware(51): frame_thread E
DEBUG/CameraService(51): CameraService::connect E (pid 1031, client 0x3b510)
DEBUG/CameraService(51): CameraService::connect X (pid 1031, new client 0x3b510) rejected. (old pid 1031, old client 0x3b2e0)
DEBUG/AndroidRuntime(1031): Shutting down VM
WARN/dalvikvm(1031): threadid=3: thread exiting with uncaught exception (group=0x4001da28)
ERROR/AndroidRuntime(1031): Uncaught handler: thread main exiting due to uncaught exception
ERROR/AndroidRuntime(1031): java.lang.RuntimeException: Fail to connect to camera service
ERROR/AndroidRuntime(1031): at android.hardware.Camera.native_setup(Native Method)
ERROR/AndroidRuntime(1031): at android.hardware.Camera.native_setup(Native Method)
ERROR/AndroidRuntime(1031): at android.hardware.Camera.open(Camera.java:67)
ERROR/AndroidRuntime(1031): at com.androidsecurity.wiia.Photographer$1.surfaceCreated(Photographer.java:78)
ERROR/AndroidRuntime(1031): at android.view.SurfaceView.updateWindow(SurfaceView.java:392)
ERROR/AndroidRuntime(1031): at android.view.SurfaceView.dispatchDraw(SurfaceView.java:264)
ERROR/AndroidRuntime(1031): at android.view.ViewGroup.drawChild(ViewGroup.java:1524)
ERROR/AndroidRuntime(1031): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1256)
ERROR/AndroidRuntime(1031): at android.view.ViewGroup.drawChild(ViewGroup.java:1524)
ERROR/AndroidRuntime(1031): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1256)
ERROR/AndroidRuntime(1031): at android.view.View.draw(View.java:6277)
ERROR/AndroidRuntime(1031): at android.widget.FrameLayout.draw(FrameLayout.java:352)
ERROR/AndroidRuntime(1031): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1883)
ERROR/AndroidRuntime(1031): at android.view.ViewRoot.draw(ViewRoot.java:1332)
ERROR/AndroidRuntime(1031): at android.view.ViewRoot.performTraversals(ViewRoot.java:1097)
ERROR/AndroidRuntime(1031): at android.view.ViewRoot.handleMessage(ViewRoot.java:1613)
ERROR/AndroidRuntime(1031): at android.os.Handler.dispatchMessage(Handler.java:99)
ERROR/AndroidRuntime(1031): at android.os.Looper.loop(Looper.java:123)
ERROR/AndroidRuntime(1031): at android.app.ActivityThread.main(ActivityThread.java:4203)
ERROR/AndroidRuntime(1031): at java.lang.reflect.Method.invokeNative(Native Method)
ERROR/AndroidRuntime(1031): at java.lang.reflect.Method.invoke(Method.java:521)
ERROR/AndroidRuntime(1031): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
ERROR/AndroidRuntime(1031): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
ERROR/AndroidRuntime(1031): at dalvik.system.NativeStart.main(Native Method)
和/data/anr/traces.txt显示:
`DALVIK THREADS:
"main" prio=5 tid=3 NATIVE
| group="main" sCount=1 dsCount=0 s=N obj=0x4001db08 self=0xbc48
| sysTid=358 nice=0 sched=0/0 handle=-1343996920
at android.os.BinderProxy.transact(Native Method)
at android.app.ActivityManagerProxy.handleApplicationError(ActivityManagerNative.java:2243)
at com.android.internal.os.RuntimeInit.crash(RuntimeInit.java:302)
at com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:75)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:887)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:884)
at dalvik.system.NativeStart.main(Native Method)
"imageCapture" prio=5 tid=17 TIMED_WAIT
| group="main" sCount=1 dsCount=0 s=N obj=0x432a66f0 self=0x1dc820
| sysTid=366 nice=0 sched=0/0 handle=1938080
at java.lang.Object.wait(Native Method)
- waiting on <0x1c8cd8> (a java.util.Timer$TimerImpl)
at java.lang.Object.wait(Object.java:326)
at java.util.Timer$TimerImpl.run(Timer.java:250)
"Binder Thread #3" prio=5 tid=15 NATIVE
| group="main" sCount=1 dsCount=0 s=N obj=0x4327aa38 self=0x160ea0
| sysTid=365 nice=0 sched=0/0 handle=1406616
at dalvik.system.NativeStart.run(Native Method)
"Binder Thread #2" prio=5 tid=13 NATIVE
| group="main" sCount=1 dsCount=0 s=N obj=0x4327a978 self=0x1609c0
| sysTid=364 nice=0 sched=0/0 handle=498392
at dalvik.system.NativeStart.run(Native Method)
"Binder Thread #1" prio=5 tid=11 NATIVE
| group="main" sCount=1 dsCount=0 s=N obj=0x432799d0 self=0x155268
| sysTid=363 nice=0 sched=0/0 handle=1397288
at dalvik.system.NativeStart.run(Native Method)
"JDWP" daemon prio=5 tid=9 VMWAIT
| group="system" sCount=1 dsCount=0 s=N obj=0x432782a0 self=0x15f788
| sysTid=361 nice=0 sched=0/0 handle=1614352
at dalvik.system.NativeStart.run(Native Method)
"Signal Catcher" daemon prio=5 tid=7 RUNNABLE
| group="system" sCount=0 dsCount=0 s=N obj=0x432781e8 self=0x18a0f0
| sysTid=360 nice=0 sched=0/0 handle=1614000
at dalvik.system.NativeStart.run(Native Method)
"HeapWorker" daemon prio=5 tid=5 VMWAIT
| group="system" sCount=1 dsCount=0 s=N obj=0x422dd3e8 self=0x1177a8
| sysTid=359 nice=0 sched=0/0 handle=1613728
at dalvik.system.NativeStart.run(Native Method)`
听起来问题是超时问题,但我不知道应该在哪里寻找....有没有人有线索?
谢谢!
答案 0 :(得分:15)
很可能是作为媒体流程一部分的cameraService在后台崩溃了。如果您只是尝试在Android中运行默认的相机应用程序并且它没有显示,则只需重新启动设备即可关闭此错误。
答案 1 :(得分:6)
答案 2 :(得分:2)
你不应该在takePicture之前调用stopPreview。
http://developer.android.com/reference/android/hardware/Camera.html
6.Important:调用startPreview()开始更新预览曲面。 必须先开始预览,然后才能开始 拍照。
private void takePicture() {
// WRONG: camera.stopPreview();
camera.takePicture(null, null, photoCallback);
}