});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
ContentResolver contentResolver = getContentResolver();
/**
* 因为两种方式都用到了startActivityForResult方法,这个方法执行完后都会执行onActivityResult方法,
* 所以为了区别到底选择了那个方式获取图片要进行判断,这里的requestCode跟startActivityForResult里面第二个参数对应
*/
if(requestCode==0){
try {
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);
cursor.close();
photoView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}else if(requestCode==1){
try {
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize=1;
if(myBitmap!=null)
myBitmap.recycle();
myBitmap = BitmapFactory
.decodeFile(photoPath,opts);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
myBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
photoView.setImageBitmap(myBitmap);
}
}