android获取手机相册和相机照片
2013-09-08 19:50
417 查看
在Android的开发过程中,我们可能会读取手机里面的照片或者通过相机拍摄获取照片,这是两种常用的获取图片的方式,在做项目过程中也会经常遇到,下面来介绍一下这两种获取方式..
1.从本地相册获取照片:
一般就是写出这个方法
protectedvoid getImageFromAlbum() {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");//相片类型
startActivityForResult(intent, REQUEST_CODE_PICK_IMAGE);
}
protectedvoid getImageFromCamera() {
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)) {
Intent getImageByCamera = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(getImageByCamera, REQUEST_CODE_CAPTURE_CAMEIA);
}
else {
Toast.makeText(getApplicationContext(), "请确认已经插入SD卡", Toast.LENGTH_LONG).show();
}
}
@Override
protectedvoid onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_PICK_IMAGE) {
Uri uri = data.getData();
} elseif (requestCode == REQUEST_CODE_CAPTURE_CAMEIA ) {
Uri uri = data.getData();
//to do find the path of pic
} }
但是,有时候我们会发现用相机拍摄获取照片的时候,得到的 uri 是 null 的,这是因为android把拍摄的图片封装到bundle中传递回来,但是根据不同的机器获得相片的方式不太一样,可能有的相机能够通过inten.getData()获取到uri,然后再根据uri获取数据的路径,在封装成bitmap,但有时候有的相机获取到的是null的,这时候我们该怎么办呢?其实这时候我们就应该从bundle中获取数据,通过(Bitmap) bundle.get("data")获取到相机图片的bitmap数据。为了能够同时适应上述两种情况,我们这时候就应该在获取图片时做判断了。我们可以在响应的时候做一个判断:
@Override
protectedvoid onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_PICK_IMAGE) {
Uri uri = data.getData();
} elseif (requestCode == REQUEST_CODE_CAPTURE_CAMEIA ) {
Uri uri = data.getData();
if(uri == null){
Bundle bundle = data.getExtras();
if (bundle != null) {
Bitmap photo = (Bitmap) bundle.get("data");
saveImage(Bitmap photo, String spath);
} else {
Toast.makeText(getApplicationContext(), "err****", Toast.LENGTH_LONG).show();
return;
}
}else{
//to do find the path of pic by uri
}
}
}
publicstaticvoid saveImage(Bitmap photo, String spath) {
try {
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(spath, false));
photo.compress(Bitmap.CompressFormat.JPEG, 100, bos);
bos.flush();
bos.close();
} catch (Exception e) {
e.printStackTrace();
returnfalse;
}
returntrue;
}
其实方式很简单,在
Intent getImageByCamera = new Intent("android.media.action.IMAGE_CAPTURE");
之后我们直接讲文件先保存到指定的路径filepath,然后直接在
onActivityResult(int requestCode, int resultCode, Intent data)
中把filepath传递过去就行了。
private String capturePath = null;
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)) {
Intent getImageByCamera = new Intent("android.media.action.IMAGE_CAPTURE");
String out_file_path = Constant.SAVED_IMAGE_DIR_PATH;
File dir = new File(out_file_path);
if (!dir.exists()) {
dir.mkdirs();
}
capturePath = Constant.SAVED_IMAGE_DIR_PATH + System.currentTimeMillis() + ".jpg";
getImageByCamera.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(capturePath)));
getImageByCamera.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(getImageByCamera, Constant.REQUEST_CODE_CAPTURE_CAMEIA);
}
else {
Toast.makeText(getApplicationContext(), "请确认已经插入SD卡", Toast.LENGTH_LONG).show();
}
}
1.从本地相册获取照片:
一般就是写出这个方法
protectedvoid getImageFromAlbum() {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");//相片类型
startActivityForResult(intent, REQUEST_CODE_PICK_IMAGE);
}
protected void getImageFromAlbum() { Intent intent = new Intent(Intent.ACTION_PICK); intent.setType("image/*");//相片类型 startActivityForResult(intent, REQUEST_CODE_PICK_IMAGE); }2.从照相机获取照片
protectedvoid getImageFromCamera() {
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)) {
Intent getImageByCamera = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(getImageByCamera, REQUEST_CODE_CAPTURE_CAMEIA);
}
else {
Toast.makeText(getApplicationContext(), "请确认已经插入SD卡", Toast.LENGTH_LONG).show();
}
}
protected void getImageFromCamera() { String state = Environment.getExternalStorageState(); if (state.equals(Environment.MEDIA_MOUNTED)) { Intent getImageByCamera = new Intent("android.media.action.IMAGE_CAPTURE"); startActivityForResult(getImageByCamera, REQUEST_CODE_CAPTURE_CAMEIA); } else { Toast.makeText(getApplicationContext(), "请确认已经插入SD卡", Toast.LENGTH_LONG).show(); } }写完这个方法后,一般我们还需要通过响应这个方法去获取图片
@Override
protectedvoid onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_PICK_IMAGE) {
Uri uri = data.getData();
} elseif (requestCode == REQUEST_CODE_CAPTURE_CAMEIA ) {
Uri uri = data.getData();
//to do find the path of pic
} }
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_CODE_PICK_IMAGE) { Uri uri = data.getData(); //to do find the path of pic } else if (requestCode == REQUEST_CODE_CAPTURE_CAMEIA ) { Uri uri = data.getData(); //to do find the path of pic } }
但是,有时候我们会发现用相机拍摄获取照片的时候,得到的 uri 是 null 的,这是因为android把拍摄的图片封装到bundle中传递回来,但是根据不同的机器获得相片的方式不太一样,可能有的相机能够通过inten.getData()获取到uri,然后再根据uri获取数据的路径,在封装成bitmap,但有时候有的相机获取到的是null的,这时候我们该怎么办呢?其实这时候我们就应该从bundle中获取数据,通过(Bitmap) bundle.get("data")获取到相机图片的bitmap数据。为了能够同时适应上述两种情况,我们这时候就应该在获取图片时做判断了。我们可以在响应的时候做一个判断:
@Override
protectedvoid onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_PICK_IMAGE) {
Uri uri = data.getData();
} elseif (requestCode == REQUEST_CODE_CAPTURE_CAMEIA ) {
Uri uri = data.getData();
if(uri == null){
Bundle bundle = data.getExtras();
if (bundle != null) {
Bitmap photo = (Bitmap) bundle.get("data");
saveImage(Bitmap photo, String spath);
} else {
Toast.makeText(getApplicationContext(), "err****", Toast.LENGTH_LONG).show();
return;
}
}else{
//to do find the path of pic by uri
}
}
}
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_CODE_PICK_IMAGE) { Uri uri = data.getData(); //to do find the path of pic by uri } else if (requestCode == REQUEST_CODE_CAPTURE_CAMEIA ) { Uri uri = data.getData(); if(uri == null){ //use bundle to get data Bundle bundle = data.getExtras(); if (bundle != null) { Bitmap photo = (Bitmap) bundle.get("data"); //get bitmap //spath :生成图片取个名字和路径包含类型 saveImage(Bitmap photo, String spath); } else { Toast.makeText(getApplicationContext(), "err****", Toast.LENGTH_LONG).show(); return; } }else{ //to do find the path of pic by uri } } }后面的过程就需要通过bitmap转化成相应的图片文件了。不过得到最终的图片是被压缩了的。
publicstaticvoid saveImage(Bitmap photo, String spath) {
try {
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(spath, false));
photo.compress(Bitmap.CompressFormat.JPEG, 100, bos);
bos.flush();
bos.close();
} catch (Exception e) {
e.printStackTrace();
returnfalse;
}
returntrue;
}
public static void saveImage(Bitmap photo, String spath) { try { BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream(spath, false)); photo.compress(Bitmap.CompressFormat.JPEG, 100, bos); bos.flush(); bos.close(); } catch (Exception e) { e.printStackTrace(); return false; } return true; }得到的图片是压缩过的,如果我们想得到相机拍摄出来的原照片,我们又应该怎样做呢?
其实方式很简单,在
Intent getImageByCamera = new Intent("android.media.action.IMAGE_CAPTURE");
之后我们直接讲文件先保存到指定的路径filepath,然后直接在
onActivityResult(int requestCode, int resultCode, Intent data)
中把filepath传递过去就行了。
private String capturePath = null;
private String capturePath = null;protectedvoid getImageFromCamera() {
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)) {
Intent getImageByCamera = new Intent("android.media.action.IMAGE_CAPTURE");
String out_file_path = Constant.SAVED_IMAGE_DIR_PATH;
File dir = new File(out_file_path);
if (!dir.exists()) {
dir.mkdirs();
}
capturePath = Constant.SAVED_IMAGE_DIR_PATH + System.currentTimeMillis() + ".jpg";
getImageByCamera.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(capturePath)));
getImageByCamera.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(getImageByCamera, Constant.REQUEST_CODE_CAPTURE_CAMEIA);
}
else {
Toast.makeText(getApplicationContext(), "请确认已经插入SD卡", Toast.LENGTH_LONG).show();
}
}
protected void getImageFromCamera() { String state = Environment.getExternalStorageState(); if (state.equals(Environment.MEDIA_MOUNTED)) { Intent getImageByCamera = new Intent("android.media.action.IMAGE_CAPTURE"); String out_file_path = Constant.SAVED_IMAGE_DIR_PATH; File dir = new File(out_file_path); if (!dir.exists()) { dir.mkdirs(); } capturePath = Constant.SAVED_IMAGE_DIR_PATH + System.currentTimeMillis() + ".jpg"; getImageByCamera.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(capturePath))); getImageByCamera.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); startActivityForResult(getImageByCamera, Constant.REQUEST_CODE_CAPTURE_CAMEIA); } else { Toast.makeText(getApplicationContext(), "请确认已经插入SD卡", Toast.LENGTH_LONG).show(); } }在onActivityResult(int requestCode, int resultCode, Intent data)中我们只要把路径filepath定义为全局的变量传送过来就行了。这样得到的图片是直接从相机中拍摄得到的照片,不会被压缩了。
相关文章推荐
- Android 从手机相册获取图片 uri 路径 从相机获取照片
- android 调用系统相册选取照片或者打开相机拍照获取图片,返回时回到手机桌面,然后才会跳转到当前activity
- android 调用系统相册选取照片或者打开相机拍照获取图片,返回时回到手机桌面,然后才会跳转到当前activity
- 如何从手机相册或相机获取照片
- Android 获取手机相册照片,返回并展示
- Android 从相册获取最近拍摄的多张照片(获取相机拍照所存储的照片)
- Android,一个猥琐的获取妹子手机相机里所有照片的办法...
- android 调用系统相册选取照片或者打开相机拍照获取图片
- android 获取相册图片和使用相机照照片 并使用Android自带图片剪裁界面剪裁
- android相机、图库获取照片,并实现两个手机对图像进行放大,缩小功能
- android 调用系统相册选取照片或者打开相机拍照获取图片,返回时为什么会回到home界面或者到前一个界面,然后才会跳转到当前activity
- Android 7.0实际开发中调用系统相机和获取相册照片遇到的坑具备向下兼容
- Android,一个猥琐的获取妹子手机相机里所有照片的办法...
- android 从相机或者相册获取照片!
- [置顶] Android 7.0实际开发中调用系统相机和获取相册照片遇到的坑具备向下兼容
- android中获取手机相机和相册可以传多张图片
- android 调用系统相册选取照片或者打开相机拍照获取图片
- android 打开相机/打开相册(版本兼容)-获取照片,压缩100K一下保存到本地
- Android调用系统相机拍照、从相册中取照片,裁剪,并保存到手机SD卡中,展示到界面
- Android相机、相册获取图片显示并保存到SD卡