1

Android用のカメラアプリを作成しようとしていますが、プレビューを機能させるのに問題があります。androidがここで提供する手順を実行しようとしましたが、機能せず、アプリがクラッシュし続けます。何が間違っているのかわかりませんが、正しい手順を実行したことは確かです。

これが私がこれまでに持っているものです:カメラアクティビティクラス

public class CameraActivity extends Activity
{
    private CameraPreview mPreview = null;
    private Camera mCamera = null;


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);

        mCamera = getCameraInstance();

        mPreview = new CameraPreview(this, mCamera);
        FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
        preview.addView(mPreview);

    }
    public static Camera getCameraInstance()
    {
        Camera c = null;

        try
        {
            //attempt to get a Camera instance
            c = Camera.open();
        }
        catch(Exception e)
        {
            //Camera is not available(in use or does not exist)
        }
        return c;//returns null if camera is unavailable
    }


    public void onPause()
    {
        super.onPause();
        mCamera.stopPreview();
        releaseCamera();
    }


    private void releaseCamera()
    {
        if(mCamera != null)
        {
            mCamera.release();
            mCamera = null;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.camera, menu);
        return true;
    }
}

カメラプレビュークラス

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback
{
    private final String TAG = "CameraPreview";
    private SurfaceHolder mHolder;
    private Camera mCamera;

    public CameraPreview(Context context, Camera camera)
    {
        super(context);
        mCamera = camera;

        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        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)
    {
        //the Surface has been created, now tell the camera where to draw the preview
            try
            {
                mCamera.setPreviewDisplay(holder);
                mCamera.startPreview();
            }
            catch(IOException e)
            {
                Log.d(TAG, "Error setting camera preview: " + e.getMessage());  
            }
    }

    public void surfaceDestroyed(SurfaceHolder holder)
    {
        //empty. take care of releasing the Camera preview in your activity
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
    {
        //if your preview can change or rotate, take care of those events here.
        //make sure to stop the preview before resizing of reformatting it.
        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
        try
        {
            //start preview with new settings
            mCamera.setPreviewDisplay(mHolder);
            mCamera.startPreview();
        }
        catch(Exception e)
        {
            Log.d(TAG, "Error starting camera preview: " + e.getMessage());
        }
    }
}

これは、カメラアクティビティの.xmlです。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

    <FrameLayout
        android:id="@+id/camera_preview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        />


    <Button
        android:id="@+id/button_capture"
        android:text="Capture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        />
</LinearLayout> 

私が知る限り、私はガイドに正しく従いました、そしてそれはうまくいくはずです。次の部分は録音に関係しているので、それまでのすべての手順で機能するプレビューが生成されるはずですが、機能しません。誰かが私が変更する必要がある何か間違っているのを見ることができますか?ありがとう

編集:マニフェストmanifest.xmlを追加しました

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ics466.project.warpwalker"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.CAMERA" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.ics466.project.warpwalker.WarpIntro"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
            </activity>
            <activity
            android:name="com.ics466.project.warpwalker.CameraActivity"
            android:label="@string/title_activity_camera" >
        </activity>
    </application>

</manifest>
4

3 に答える 3

0

android:minSdkVersion="8"する必要があります android:minSdkVersion="9"

if you have used some functions which are added in API 9

and Read this link How To Enable Camera in Android Emulator

Or test this app in a Real Device which has a camera.

于 2013-03-23T04:43:42.657 に答える
0

On most devices, the default orientation of the camera preview is landscape.

Try adding android:orientation="horizontal" in to your .xml file's LinearLayout.

于 2013-03-23T04:51:39.360 に答える
0

try this
Give the CameraId as integer
Camera.open(0);
http://developer.android.com/reference/android/hardware/Camera.html#open(int)

于 2014-05-26T05:33:05.277 に答える