我想创建一个应用程序,允许过敏的人拍摄他们的反应,当他们有一些过敏反应,以及他们吃的食物。然后他们可以将信息添加到文档中,并保存所有这些信息,以及地理定位。(这个想法是,一个过敏的人需要一个日记,因为他通常每6个月可以看到一次他的过敏原)
我写了这样的代码:我必须点击手机的菜单按钮,这样我才能使用相机。但是,我现在想要有一个按钮,让我可以使用摄像头。有没有人可以帮我修改我的代码,帮我添加一个正确的按钮,这样我就可以用我的android手机拍照了?
忠实地,
我的代码:
打包android.camera;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ImageView;
public class CameraActivity extends Activity {
private static final int PICTURE_RESULT = 9;
private Bitmap mPicture;
private ImageView mView;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
mView = (ImageView) findViewById(R.id.view);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.camera:
Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
CameraActivity.this.startActivityForResult(camera, PICTURE_RESULT);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/* public void onDestroy() {
super.onDestroy();
if (mPicture != null) {
mPicture.recycle();
}
}*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// if results comes from the camera activity
if (requestCode == PICTURE_RESULT) {
// if a picture was taken
if (resultCode == Activity.RESULT_OK) {
// Free the data of the last picture
if(mPicture != null)
mPicture.recycle();
// Get the picture taken by the user
mPicture = (Bitmap) data.getExtras().get("data");
// Avoid IllegalStateException with Immutable bitmap
Bitmap pic = mPicture.copy(mPicture.getConfig(), true);
mPicture.recycle();
mPicture = pic;
// Show the picture
mView.setImageBitmap(mPicture);
// if user canceled from the camera activity
} else if (resultCode == Activity.RESULT_CANCELED) {
}
}
}
}发布于 2012-05-12 01:37:58
我想这就是你要问的..。
在布局文件(main.xml)中添加一个按钮:
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Take Picture" android:id="@+id/take_picture"></Button>然后在你的onCreate方法中:
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
mView = (ImageView) findViewById(R.id.view);
mTakePictureButton = (Button) findViewById(R.id.take_picture);
mTakePictureButton.setClickable(true);
mTakePictureButton.setOnClickListener(new View.onClickListener(){
@Override
public void onClick(View v) {
Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(camera, PICTURE_RESULT);
}
});
}https://stackoverflow.com/questions/10553877
复制相似问题