2015-11-04 94 views
0

为什么我在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; 


    } 
} 
+2

那是什么错误? –

+0

它应该工作,在'logcat'中看到的错误是什么? – OBX

回答

0

试试这个,这应该工作,代码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> 
0

尝试输出路径添加到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添加以下代码段。

相关问题