如何解决图像捕获代码中的错误?

How to solve the error in the image capture code?

为什么我在 button.setOnClickListener(new OnClickListener() 上出错。我正在尝试在 ImageCapture 上创建一个项目。我得到一些 error.Below 是我的代码,

public class MainActivity extends Activity {
    private static final int CAMARA_REQUEST = 1888;

    ImageView imageView;
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
      // TODO Auto-generated method stub
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      imageView = (ImageView)findViewById(R.id.imageView1);
      button = (Button)findViewById(R.id.button1);

      button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);  
            startActivityForResult(cameraIntent, CAMARA_REQUEST );  

        }
    });


    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        if (requestCode == CAMARA_REQUEST ) {  
            Bitmap photo = (Bitmap) data.getExtras().get("data");  
            imageView.setImageBitmap(photo);  


        }

    }


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


    }
}

尝试将输出路径添加到 intent

 Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
 takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,file_path));
 startActivityForResult(takePictureIntent, code);

并在清单中添加以下代码段

  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  <uses-feature
        android:name="android.hardware.camera"
        android:required="true" />

官方document.

试试这个,这应该有效,代码为 MainActivity

    public class MainActivity extends ActionBarActivity {
   Button b1,b2;
   ImageView iv;

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

      b1=(Button)findViewById(R.id.button);
      iv=(ImageView)findViewById(R.id.imageView);

      b1.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent, 0);
         }
      });
   }

   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      // TODO Auto-generated method stub
      super.onActivityResult(requestCode, resultCode, data);

      Bitmap bp = (Bitmap) data.getExtras().get("data");
      iv.setImageBitmap(bp);
   }

   @Override
   protected void onDestroy() {
      super.onDestroy();
   }

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

   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.

      int id = item.getItemId();

      //noinspection SimplifiableIfStatement
      if (id == R.id.action_settings) {
         return true;
      }
      return super.onOptionsItemSelected(item);
   }
}

和主 XML 文件的代码:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
   android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

   <TextView android:text="Camera Example" android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/textview"
      android:textSize="35dp"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true" />

   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Tutorials point"
      android:id="@+id/textView"
      android:layout_below="@+id/textview"
      android:layout_centerHorizontal="true"
      android:textColor="#ff7aff24"
      android:textSize="35dp" />

   <ImageView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/imageView"
      android:src="@drawable/abc"
      android:layout_below="@+id/textView"
      android:layout_centerHorizontal="true" />

   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="camera"
      android:id="@+id/button"
      android:layout_below="@+id/imageView"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="86dp" />

</RelativeLayout>