カメラアプリを起動しようとした所エミュレーター上にUnfortunately.(アプリ名)has stopped というエラーが出ます logcatには java.lang.RuntimeException: Fail to connect to camera serviceこのように出ます
ソースは2つ載せておきます。
java01
1package sample.android.camera01; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.view.Window; 6 import android.view.WindowManager; 7 import android.widget.LinearLayout; 8 9public class SampleCamera01 extends Activity { 10 /** Called when the activity is first created. */ 11 @Override 12 public void onCreate(Bundle savedInstanceState) { 13 super.onCreate(savedInstanceState); 14 getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 15 requestWindowFeature(Window.FEATURE_NO_TITLE); 16 17 LinearLayout l = new LinearLayout(this); 18 l.addView(new SampleView(this)); 19 setContentView(l); 20 } 21}
java02
1package sample.android.camera01; 2 3 4 5 import java.io.IOException; 6 7 import android.content.Context; 8 import android.hardware.Camera; 9 import android.view.SurfaceHolder; 10 import android.view.SurfaceHolder.Callback; 11 import android.view.SurfaceView; 12 13public class SampleView extends SurfaceView implements Callback { 14 private Camera camera; 15 16 public SampleView(Context context) { 17 super(context); 18 SurfaceHolder holder = getHolder(); 19 holder.addCallback(this); 20 holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 21 } 22 23 @Override 24 public void surfaceCreated(SurfaceHolder holder) { 25 try { 26 camera = Camera.open(); 27 camera.setPreviewDisplay(holder); 28 } catch(IOException e) { 29 } 30 } 31 32 @Override 33 public void surfaceChanged(SurfaceHolder holder, int f, int w, int h) { 34 Camera.Parameters p = camera.getParameters(); 35 p.setPreviewSize(w,h); 36 camera.setParameters(p); 37 camera.startPreview(); 38 } 39 40 @Override 41 public void surfaceDestroyed(SurfaceHolder holder) { 42 camera.stopPreview(); 43 camera.release(); 44 } 45 46}
追記
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="sample.android.camera01" android:versionCode="1" android:versionName="1.0"> <application android:label="@string/app_name"> <activity android:name=".SampleCamera01" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="4" /> <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.WRIGHT_EXTERNAL_STORAGE" /> </manifest>
回答4件
あなたの回答
tips
プレビュー