您的位置:首页 > 移动开发 > Android开发

android打开系统相机分别获得原图和缩略图

2016-12-08 15:27 459 查看
第一种:获得缩略图:

打开相机

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);


权限:

<uses-permission Android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: