第一种:获得缩略图:
打开相机
private void photo() {
Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(openCameraIntent, TAKE_PICTURE);
}
这里使用data得到的只是缩略图,大小一般30k左右,一般用在手机展示如头像这种小图,想上传图片数据到服务器就不合适了!
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case TAKE_PICTURE:
String fileName = String.valueOf(System.currentTimeMillis());
Bitmap bm = (Bitmap) data.getExtras().get("data");
String path = FileUtils.saveBitmap(bm, fileName);
if (!StringUtil.isBland(path)) {
String decodePath = FileUtils.saveBitmap(UIUtils.decodeBitmap(path), "decode" + fileName);
if (!StringUtil.isBland(decodePath)) {
selectImagesDecodePath.add(decodePath);
selectImagesPath.add(path);
showAdapter();
}
}
break;
}
}
super.onActivityResult(requestCode, resultCode, data);
}
第二种:获得原图
打开相机
private void photo() {
File dir = new File(Environment.getExternalStorageDirectory() + "/DataCollect/");
if (!dir.exists()) dir.mkdirs();
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
localTempImg = System.currentTimeMillis();
localTempImgFileName = localTempImg + ".jpg";
File f = new File(dir, localTempImgFileName);
Uri u = Uri.fromFile(f);
intent.putExtra(MediaStore.Images.Media.ORIENTATION, 0);
intent.putExtra(MediaStore.EXTRA_OUTPUT, u);
startActivityForResult(intent, TAKE_PICTURE);
}
这里的f.getAbsolutePath()就是原图的路径了:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case TAKE_PICTURE:
File f = new File(Environment.getExternalStorageDirectory()
+ "/DataCollect/" + localTempImgFileName);
LogUtils.e(f.getAbsolutePath());
if (!StringUtil.isBland(f.getAbsolutePath())) {
String decodePath = FileUtils.saveBitmap(UIUtils.decodeBitmap(f.getAbsolutePath()), "decode" + localTempImg);
if (!StringUtil.isBland(decodePath)) {
selectImagesDecodePath.add(decodePath);
selectImagesPoth.add(f.getAbsolutePath());
showAdapter();
}
}
break;
}
}
super.onActivityResult(requestCode, resultCode, data);
}
这里可能用到他的Uri数据,
Uri u=Uri.parse(android.provider.MediaStore.Images.Media.insertImage(getContentResolver(),
f.getAbsolutePath(), null, null));
由相机或图库中的图片bitmap与uri互相转换
1、bitmap to uri
Uri uri = Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, null,null));
2、uri to bitmap
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
权限: