Search code examples
androidandroid-camera-intent

How to solve the error in the image capture code?


why do I get error on button.setOnClickListener(new OnClickListener() . I am trying to make a project on ImageCapture. I am getting some error.Below is my code,

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;  


    }
}

Solution

  • Try this, this should work , the code to 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);
       }
    }
    

    and the code to the main XML file:

        <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>