方法一:
此方法会由Camera直接产生照片回传给应用程序,但是返回的是压缩图片,显示不清晰
/** 启动Camera */
private void intentCamera(){
try {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 0);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
}
/** 在onActivityResult中获取图片 */
private void getImgFromCamera(){
Bundle bundle = data.getExtras();
bm = (Bitmap) bundle.get("data");
if (bm != null)
bm.recycle();
bm = (Bitmap) data.getExtras().get("data");
if(bm != null){
img.setImageBitmap(bm);
}
}
方法二:
此方法所拍即所得,但是会在Sd卡上产生临时文件
/*** 打开照相机 */
private void intentCamera(){
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory() + "/Images");
if(!file.exists()){
file.mkdirs();
}
Uri mUri = Uri.fromFile(
new File(Environment.getExternalStorageDirectory() + "/Images/",
"cameraImg" + String.valueOf(System.currentTimeMillis()) + ".jpg"));
cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mUri);
cameraIntent.putExtra("return-data", true);
startActivityForResult(cameraIntent, 1);
}
/*** 获取相机返回的数据 */
private void getImgFromCamera(){
ContentResolver cr = this.getContentResolver();
try {
if(cameraBitmap != null)
cameraBitmap.recycle();// 如果不释放的话,不断取图片,将会内存不够
cameraBitmap = BitmapFactory.decodeStream(cr.openInputStream(mUri));
if(bm != null){
img.setImageBitmap(bm);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
Log.e("error", "从相机中获取图片失败=====");
e.printStackTrace();
}
}