选择获取方式
CharSequence[] items = {"相册", "相机"};
new AlertDialog.Builder(this)
.setTitle("选择图片来源")
.setItems(items, new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if( which == SELECT_PICTURE ){
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "选择图片"), SELECT_PICTURE);
}else{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, SELECT_CAMER);
}
}
})
.create().show
Intent方法回掉
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
//选择图片
Uri uri = data.getData();
ContentResolver cr = this.getContentResolver();
try {
if(bmp != null)//如果不释放的话,不断取图片,将会内存不够
bmp.recycle();
bmp = BitmapFactory.decodeStream(cr.openInputStream(uri));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("the bmp toString: " + bmp);
imageSV.setBmp(bmp);
}else{
Toast.makeText(SetImageActivity.this, "请重新选择图片", Toast.LENGTH_SHORT).show();
}
}