1.在Android中使用相机的方法,是直接使用intent:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1);
然后在onActivityResult直接取出图片,这里系统为了防止oom使用的是缩略图,可以放心使用。
2.如果想使用原图的话,可以使用uri传递一个地址进去,将拍摄的照片存放到传入的地址中,代码如下:
mfilePath = Environment.getExternalStorageDirectory().getPath();
mfilePath = mfilePath + "/" + "tempfile.png";
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri photouri = Uri.fromFile(new File(mfilePath));
intent.putExtra(MediaStore.EXTRA_OUTPUT, photouri);
startActivityForResult(intent, 2);
得到的结果:
/**
* data中使用的是缩略图,防止溢出
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
Bundle bundle = data.getExtras();
Bitmap bit = (Bitmap) bundle.get("data");
image.setImageBitmap(bit);
} else if (requestCode == 2) {
FileInputStream input = null;
try {
input = new FileInputStream(mfilePath);
Bitmap bit = BitmapFactory.decodeStream(input);
bit = Bitmap.createBitmap(bit, 0, 0, 720, 1280);
image.setImageBitmap(bit);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (input != null) {
input.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
super.onActivityResult(requestCode, resultCode, data);
}