我是一个初学者的android程序员,我的相机有问题。我想做的是在相机上进行预览。问题是,Camera.open() 总是返回 null。代码如下:
CameraPreview.java
public class CameraPreview extends Activity implements SurfaceHolder.Callback
{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera_preview);
SurfaceHolder holder;
SurfaceView preview = (SurfaceView) findViewById(R.id.surface1);
holder = preview.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
Camera camera = Camera.open();
if(camera!=null)
{
try
{
camera.setPreviewDisplay(holder);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
camera.startPreview();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
}
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.paparazzi"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.CAMERA" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".CameraPreview"> </activity>
</application>
模拟器配置:
hw.lcd.density=240
hw.cpu.arch=arm
skin.name=WVGA800
sdcard.size=64M
abi.type=armeabi-v7a
hw.camera.back=emulated
image.sysdir.1=system-images\android-16\armeabi-v7a\
hw.gpu.enabled=yes
hw.camera.front=emulated
skin.path=platforms\android-16\skins\WVGA800
hw.cpu.model=cortex-a8
vm.heapSize=48
hw.ramSize=512
我使用带有android插件和最新sdk的eclipse。我真的很感激任何建议。