ジンジャーブレッド 2.3.3 電話でカメラのプレビューを表示しようとしています。しかし、アクティビティが開始されるたびに、デバイスでアプリケーションがクラッシュします。
ここに私のxmlがあります:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="10dp">
<ImageView
android:id="@+id/loadbtn"
android:src="@drawable/loadbtn"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="10dp"
/>
<ImageView
android:id="@+id/capturebtn"
android:src="@drawable/capturebtn"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="10dp"
/>
<ImageView
android:id="@+id/editbtn"
android:src="@drawable/editbtn"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="10dp"
/>
<ImageView
android:id="@+id/closebtn"
android:src="@drawable/closebtn"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="10dp"
android:onClick="backToStart"
/>
</LinearLayout>
<SurfaceView
android:id="@+id/camerapreview"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
/>
そして私のコード:
public class PhotoHome extends Activity implements SurfaceHolder.Callback {
Camera camera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
boolean previewing = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_photo_home);
getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
}
public void backToStart(View v)
{
finish();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
if(previewing){
camera.stopPreview();
previewing = false;
}
if (camera != null){
Parameters parameters = camera.getParameters();
Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
if(display.getRotation() == Surface.ROTATION_0)
{
parameters.setPreviewSize(height, width);
camera.setDisplayOrientation(90);
}
if(display.getRotation() == Surface.ROTATION_90)
{
parameters.setPreviewSize(width, height);
}
if(display.getRotation() == Surface.ROTATION_180)
{
parameters.setPreviewSize(height, width);
}
if(display.getRotation() == Surface.ROTATION_270)
{
parameters.setPreviewSize(width, height);
camera.setDisplayOrientation(180);
}
camera.setParameters(parameters);
try
{
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
previewing = true;
}
catch(Exception e)
{
Log.d("Error", "Cannot start preview", e);
}
}
}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
// TODO Auto-generated method stub
camera = Camera.open();
try {
camera.setPreviewDisplay(surfaceHolder);
} catch (IOException e) {
camera.release();
camera = null;
}
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
if (camera != null) {
camera.stopPreview();
camera.setPreviewCallback(null);
camera.release();
camera = null;
}
}
ここに私の許可があります:
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
画面の特定の 300 x 300 領域にカメラのプレビューを表示しようとしています。しかし、私が言ったように、アプリはデバイス上でクラッシュしますが、エミュレーターでは、300 x 300 領域の白い背景のために機能しているようです。何か案は?
更新: Logcat
02-20 23:59:34.969: E/AndroidRuntime(15365): FATAL EXCEPTION: main
02-20 23:59:34.969: E/AndroidRuntime(15365): java.lang.RuntimeException: setParameters failed
02-20 23:59:34.969: E/AndroidRuntime(15365): at android.hardware.Camera.native_setParameters(Native Method)
02-20 23:59:34.969: E/AndroidRuntime(15365): at android.hardware.Camera.setParameters(Camera.java:1070)
02-20 23:59:34.969: E/AndroidRuntime(15365): at com.example.photoexpress.PhotoHome.surfaceChanged(PhotoHome.java:77)
02-20 23:59:34.969: E/AndroidRuntime(15365): at android.view.SurfaceView.updateWindow(SurfaceView.java:558)
02-20 23:59:34.969: E/AndroidRuntime(15365): at android.view.SurfaceView.dispatchDraw(SurfaceView.java:350)
02-20 23:59:34.969: E/AndroidRuntime(15365): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
02-20 23:59:34.969: E/AndroidRuntime(15365): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
02-20 23:59:34.969: E/AndroidRuntime(15365): at android.view.View.draw(View.java:6905)
02-20 23:59:34.969: E/AndroidRuntime(15365): at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
02-20 23:59:34.969: E/AndroidRuntime(15365): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
02-20 23:59:34.969: E/AndroidRuntime(15365): at android.view.View.draw(View.java:6905)
02-20 23:59:34.969: E/AndroidRuntime(15365): at android.widget.FrameLayout.draw(FrameLayout.java:357)
02-20 23:59:34.969: E/AndroidRuntime(15365): at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
02-20 23:59:34.969: E/AndroidRuntime(15365): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
02-20 23:59:34.969: E/AndroidRuntime(15365): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
02-20 23:59:34.969: E/AndroidRuntime(15365): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
02-20 23:59:34.969: E/AndroidRuntime(15365): at android.view.View.draw(View.java:6905)
02-20 23:59:34.969: E/AndroidRuntime(15365): at android.widget.FrameLayout.draw(FrameLayout.java:357)
02-20 23:59:34.969: E/AndroidRuntime(15365): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1895)
02-20 23:59:34.969: E/AndroidRuntime(15365): at android.view.ViewRoot.draw(ViewRoot.java:1542)
02-20 23:59:34.969: E/AndroidRuntime(15365): at android.view.ViewRoot.performTraversals(ViewRoot.java:1269)
02-20 23:59:34.969: E/AndroidRuntime(15365): at android.view.ViewRoot.handleMessage(ViewRoot.java:1883)
02-20 23:59:34.969: E/AndroidRuntime(15365): at android.os.Handler.dispatchMessage(Handler.java:99)
02-20 23:59:34.969: E/AndroidRuntime(15365): at android.os.Looper.loop(Looper.java:130)
02-20 23:59:34.969: E/AndroidRuntime(15365): at android.app.ActivityThread.main(ActivityThread.java:3737)
02-20 23:59:34.969: E/AndroidRuntime(15365): at java.lang.reflect.Method.invokeNative(Native Method)
02-20 23:59:34.969: E/AndroidRuntime(15365): at java.lang.reflect.Method.invoke(Method.java:507)
02-20 23:59:34.969: E/AndroidRuntime(15365): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
02-20 23:59:34.969: E/AndroidRuntime(15365): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:660)
02-20 23:59:34.969: E/AndroidRuntime(15365): at dalvik.system.NativeStart.main(Native Method)
02-20 23:59:34.979: E/AndroidRuntime(15365): [Blue Error Handler] Make Debugging Report file for main
02-20 23:59:34.979: E/AndroidRuntime(15365): java.lang.RuntimeException: setParameters failed
02-20 23:59:34.979: E/AndroidRuntime(15365): at android.hardware.Camera.native_setParameters(Native Method)
02-20 23:59:34.979: E/AndroidRuntime(15365): at android.hardware.Camera.setParameters(Camera.java:1070)
02-20 23:59:34.979: E/AndroidRuntime(15365): at com.example.photoexpress.PhotoHome.surfaceChanged(PhotoHome.java:77)
02-20 23:59:34.979: E/AndroidRuntime(15365): at android.view.SurfaceView.updateWindow(SurfaceView.java:558)
02-20 23:59:34.979: E/AndroidRuntime(15365): at android.view.SurfaceView.dispatchDraw(SurfaceView.java:350)
02-20 23:59:34.979: E/AndroidRuntime(15365): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
02-20 23:59:34.979: E/AndroidRuntime(15365): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
02-20 23:59:34.979: E/AndroidRuntime(15365): at android.view.View.draw(View.java:6905)
02-20 23:59:34.979: E/AndroidRuntime(15365): at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
02-20 23:59:34.979: E/AndroidRuntime(15365): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
02-20 23:59:34.979: E/AndroidRuntime(15365): at android.view.View.draw(View.java:6905)
02-20 23:59:34.979: E/AndroidRuntime(15365): at android.widget.FrameLayout.draw(FrameLayout.java:357)
02-20 23:59:34.979: E/AndroidRuntime(15365): at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
02-20 23:59:34.979: E/AndroidRuntime(15365): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
02-20 23:59:34.979: E/AndroidRuntime(15365): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
02-20 23:59:34.979: E/AndroidRuntime(15365): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
02-20 23:59:34.979: E/AndroidRuntime(15365): at android.view.View.draw(View.java:6905)
02-20 23:59:34.979: E/AndroidRuntime(15365): at android.widget.FrameLayout.draw(FrameLayout.java:357)
02-20 23:59:34.979: E/AndroidRuntime(15365): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1895)
02-20 23:59:34.979: E/AndroidRuntime(15365): at android.view.ViewRoot.draw(ViewRoot.java:1542)
02-20 23:59:34.979: E/AndroidRuntime(15365): at android.view.ViewRoot.performTraversals(ViewRoot.java:1269)
02-20 23:59:34.979: E/AndroidRuntime(15365): at android.view.ViewRoot.handleMessage(ViewRoot.java:1883)
02-20 23:59:34.979: E/AndroidRuntime(15365): at android.os.Handler.dispatchMessage(Handler.java:99)
02-20 23:59:34.979: E/AndroidRuntime(15365): at android.os.Looper.loop(Looper.java:130)
02-20 23:59:34.979: E/AndroidRuntime(15365): at android.app.ActivityThread.main(ActivityThread.java:3737)
02-20 23:59:34.979: E/AndroidRuntime(15365): at java.lang.reflect.Method.invokeNative(Native Method)
02-20 23:59:34.979: E/AndroidRuntime(15365): at java.lang.reflect.Method.invoke(Method.java:507)
02-20 23:59:34.979: E/AndroidRuntime(15365): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
02-20 23:59:34.979: E/AndroidRuntime(15365): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:660)
02-20 23:59:34.979: E/AndroidRuntime(15365): at dalvik.system.NativeStart.main(Native Method)
私の Logcat では、この行で設定されたパラメーターが失敗したと言われています。camera.setParameters(パラメータ); このコードを使用して、カメラのプレビューをゲームの背景として設定しました。しかし、何が起こったのかよくわかりませんし、今はうまくいきません。:(