我是android新手。我想创建一个应用程序,我可以使用camera
对象拍摄图片。这是我的简单代码
public class Main extends Activity {
Camera camera;
ImageView iv;
Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
camera = camera.open();
b = (Button) findViewById(R.id.bStartCamera);
iv = (ImageView) findViewById(R.id.imageView1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if (camera != null) {
camera.takePicture(null, null, new CallBack());
} else {
Log.d("", "Camera Object is Null");
}
}
});
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
if (camera != null) {
camera.release();
camera = null;
}
}
class CallBack implements PictureCallback {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
Log.d("", "In the Callback Method");
}
}
}
的Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.homescreensetter"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="10" />
<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.example.homescreensetter.Main"
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>
</manifest>
我在我的设备上测试了这个代码,这个代码有一个后置摄像头,但是当我拍照时,回调方法onPictureTaken
从未被调用过。请各种帮助,非常感谢,谢谢