2011-08-09 292 views

回答

4

在这里拍摄ImageView的照片画面的代码试试它

这里是我的xml文件

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="fill_parent" android:id="@+id/linear"> 
    <Button android:id="@+id/btn" android:layout_width="200dp" 
     android:layout_height="wrap_content" android:text="Click" /> 
    <ImageView android:id="@+id/img_preview" 
     android:layout_width="fill_parent" android:layout_height="fill_parent" /> 
</LinearLayout> 

这里是活动代码

Button btn; 
int CAMERA_PIC_REQUEST = 0; 
ImageView imgView; 

imgView = (ImageView) findViewById(R.id.img_preview); 

    btn = (Button) findViewById(R.id.btn); 
    btn.setCompoundDrawables(null, getResources().getDrawable(R.drawable.icon), null, null); 
    btn.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent camera_intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
      startActivityForResult(camera_intent, CAMERA_PIC_REQUEST); 
        } 
     }); 

这里是onActivityResult

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    switch(requestCode){ 
    case CAMERA_PIC_REQUEST: 
     if(resultCode==RESULT_OK){ 
      Bitmap thumbnail = (Bitmap) data.getExtras().get("data"); 
      imgView.setImageBitmap(thumbnail); 
      } 
    } 
} 
+0

我想用前置摄像头 – mohan

+0

检查此拍照http://stackoverflow.com/questions/2779002/how-to-open-front-camera-on-android-platform – Pratik

+0

你能否把你的“案例0:”改为“case CAMERA_PIC_REQUEST:” – alicanbatur

2

或学习从机器人here的API演示,

0

试试这个

public class TakingImage extends AppCompatActivity { 

private static final int CAMERA_REQUEST = 1888; 
private ImageView imageView; 

public void onCreate(Bundle savedInstance){ 
    super.onCreate(savedInstance); 
    setContentView(R.layout.activity_choosing_photo); 
    button = (Button)findViewById(R.id.button_select); 
    imageView = (ImageView)findViewById(R.id.image_view); 
    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
    startActivityForResult(cameraIntent, CAMERA_REQUEST); 
} 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) { 
     Bitmap photo = (Bitmap) data.getExtras().get("data"); 
     imageView.setImageBitmap(photo); 
    } 
} 

activity_choosing_photo.xml

<ImageView 
    android:id="@+id/image_view" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/flower" 
    android:layout_marginLeft="100dp"/> 
<Button 
    android:id="@+id/button_select" 
    android:layout_below="@+id/image_view" 
    android:layout_marginTop="40dp" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Select" 
    android:layout_marginLeft="140dp"/> 
相关问题