1,调用手机相册获取图片:(一个不错的链接:http://www.cnblogs.com/franksunny/archive/2011/11/17/2252926.html)
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);// intent.addCategory(Intent.CATEGORY_OPENABLE); //(这句可以加,也可以不加,加上的话会调用你手机上的所有能打开图片的应用供你选择,不加的话会直 接调用图库)
intent.setType("image/*");
startActivityForResult(intent, 1);
然后在
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if(requestCode==1){
if(data.getExtras()!=null){
Bitmap bt = (Bitmap) data.getExtras().get("data");
}
}
super.onActivityResult(requestCode, resultCode, data);
}
这种方式在几次试验中data.getExtras()得到的值总是为空,此时只能用另一种方式获取图片,直接找图片的源地址
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Uri selectedImage = data.getData();//得到源图片的uri
String[] filePathColumns={MediaStore.Images.Media.DATA};
Cursor c = this.getContentResolver().query(selectedImage, filePathColumns, null,null, null);
c.moveToFirst();
int columnIndex = c.getColumnIndex(filePathColumns[0]);
String picturePath= c.getString(columnIndex);//得到图片的物理地址
c.close();
}
调用下边的方法获得压缩后的图片(因为图片过大的话会无法显示在控件上)
/**
* 对图片进行压缩,根据地址获取图片,得到的图片是压缩后的图片
* @param path
* @return
* @throws IOException
*/
public static Bitmap revitionImageSize(String path) throws IOException {
BufferedInputStream in = new BufferedInputStream(new FileInputStream(
new File(path)));
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(in, null, options);//获取bitmap的参数配?
in.close();
int i = 0;
Bitmap bitmap = null;
while (true) {
if ((options.outWidth >> i <= 1000)
&& (options.outHeight >> i <= 1000)) {
in = new BufferedInputStream(
new FileInputStream(new File(path)));
options.inSampleSize = (int) Math.pow(2.0D, i);
options.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeStream(in, null, options);
break;
}
i += 1;
}
return bitmap;
}
2调用相机获取图片:
private void takePhoto() {
picCapturePath = GloableParams.picPath + LogUtil.getStrTime()+".jpg";//设置图片保存的路径,最好不要设置在data/data/package/下,因为有可能会导致拍照后无法返回到调用页面
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
Uri uri = Uri.fromFile(new File(picCapturePath));
// intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); //保存大照片,缺点:此方法除了在指定的目录下保存图片外,在系统拍照路径下也会保存下一张相同的图片
startActivityForResult(intent, ConstantValue.TAKE_PHOTO);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
String filename = "";
try {
if (resultCode == Activity.RESULT_OK && requestCode == TAKE_PICTURE) {
Bundle bundle = data.getExtras();
Bitmap bmp = (Bitmap)bundle.get("data");
filename = saveImage(bmp);
}
} catch (Exception err) {
Log.e("Exception", err.getLocalizedMessage());
}
}
public String buildFileName(){
Date now = new Date();
SimpleDateFormat formater = new SimpleDateFormat("yyMMdd_HHmmss");
String SDCARD = Environment.getExternalStorageDirectory() + "/";
String basePath = SDCARD + "dier/photo/";
File dir = new File(basePath);
dir.mkdirs();
return basePath + formater.format(now) + ".jpg";
}
public String saveImage(Bitmap bmp) throws Exception {
File file = new File(buildFileName());
file.createNewFile();
FileOutputStream oStream = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.JPEG, 100, oStream); //100是照片质量,0-100,越大越好
oStream.flush();
oStream.close();
return file.getPath();
}
使用上面的方法,保存下来的图片仅为原图的1/4大小,如果需要保存大照片则需要加上 intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
缺点:此方法除了在指定的目录下保存图片外,在系统拍照路径下也会保存下一张相同的图片