内置照相机获取图像
意图:
Intent i = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(i, REQ_CAMERA);
从Camera获取返回的数据:
Bundle bundle = data.getExtras();
Bitmap bitmap = (Bitmap) bundle.get("data");
iv_camera_img.setImageBitmap(bitmap);
这种方式显示的图片并不是原图,是一个系统处理过的图要想获得原图:
意图:
imagePath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/image.jpg";//获取图片路径
File file = new File(imagePath);
Uri imageuri = Uri.fromFile(file);
Intent i = new Intent("android.media.action.IMAGE_CAPTURE");
i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageuri);
startActivityForResult(i, REQ_CAMERA_BIG);
获取图片显示:
Display display = getWindowManager().getDefaultDisplay();
int dw = display.getWidth();
int dh = display.getHeight();
//加载图像的尺寸而不是本身
BitmapFactory.Options bfo =new BitmapFactory.Options();
bfo.inJustDecodeBounds = true;
Bitmap btm = BitmapFactory.decodeFile(imagePath, bfo);
int ratiow = (int) Math.ceil(bfo.outWidth/(float)dw);
int ratioh = (int) Math.ceil(bfo.outHeight/(float)dh);
if(ratiow>1&&ratioh>1){
if(ratioh>ratiow){
bfo.inSampleSize = ratioh; //产生一副原始图像的1/ratioh的图像
}else{
bfo.inSampleSize = ratiow;
}
}
bfo.inJustDecodeBounds = false;
btm = BitmapFactory.decodeFile(imagePath, bfo);
iv_camera_img.setImageBitmap(btm);
可以获取更大的图片显示。
记得要加读写权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />