今天记录一下如何从,相册和相机获取图片,这个部分也是android 开发中经常用的。获取到相片以后图片的处理就不再这里赘述了,我之前的微博中有详细介绍,大家可以查看。
//启动系统相机
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1);
//系统相册获取方式
Intent intentPick = new Intent(Intent.ACTION_PICK);
intentPick.setType("image/*");// 相片类型
startActivityForResult(intentPick, 2);
//获取到相片的回调
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == 1) {
Bundle bundle = data.getExtras();
Bitmap bitmap = (Bitmap) bundle.get("data");// 获取相机返回的数据,并转换为Bitmap图 //片格式
imageView.setImageBitmap(bitmap);// 将图片显示在ImageView里
} else {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
Log.d("aas", "........." + picturePath);
cursor.close();
Bitmap bitmap = ImageUtil.getBitmapFromFilePath(picturePath,100, 100);//自己封 //装的类
imageView.setImageBitmap(bitmap);
}
}
}