0

我想创建一个应用程序,让过敏的人在他们有一些过敏反应时拍摄他们的反应,以及他们吃的食物。然后他们可以将信息添加到文档中,并保存所有这些信息以及地理定位。(这个想法是过敏的人需要日记,因为通常他每 6 个月可以看到一次他的过敏原)

我已经编写了这个有效的代码:我必须点击手机的菜单按钮才能使用相机。但是,我现在想要一个按钮,让我可以使用摄像头。任何人都可以通过更改我的代码并帮助我正确添加一个允许使用相机的按钮来帮助我,以便我可以用我的安卓手机拍照吗?

忠实,

我的代码:

包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) {

                    }
                }
            }
        }
4

1 回答 1

0

我想这就是你要问的...

在您的布局文件 (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);

        }
            });
        }
于 2012-05-11T17:37:57.960 回答