0

我知道呼叫系统摄像头是

Intent intent = new Intent();  
intent.setAction("android.media.action.STILL_IMAGE_CAMERA");   
startActivity(intent);

我想在 ImageView 中显示图片,但我不知道该怎么做。有没有人可以给我一个示例代码?

编辑1:我也需要商店

String strPath = Environment.getExternalStorageDirectory().toString();
  File path = new File(strPath);
  if(!path.exists())
  path.mkdirs();
  strFileName = "test.jpg";
  strFilePath = strPath + "/" + strFileName;

  File file = new File(strFilePath);
  Uri uri = Uri.fromFile(file);
  intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);

我用up代码来存储,但是在拍照的时候怎么看呢?那就是预览。

4

1 回答 1

0

需要用startActivityForResult代替startActivity,然后在onActivityResult中抓取结果。像这样的东西:

public class MyCameraActivity extends Activity {
private static final int CAMERA_REQUEST = 1888; 
private ImageView imageView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    this.imageView = (ImageView)this.findViewById(R.id.imageView1);
    Button photoButton = (Button) this.findViewById(R.id.button1);
    photoButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            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);
    }  
} 

}

于 2012-10-10T06:14:42.883 回答