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

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

}

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获取照片
相关文章推荐