public class PhotoChoose extends Activity {
private Button myButton01;
private ImageView myImageView01;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.photo_choose);
myImageView01 = (ImageView) findViewById(R.id.imageView1);
myButton01 = (Button) findViewById(R.id.button1);
myButton01.setOnClickListener(new Button.OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent();
/* 开启Pictures画面Type设定为image */
intent.setType("image/*");
/* 使用Intent.ACTION_GET_CONTENT这个Action */
intent.setAction(Intent.ACTION_GET_CONTENT);
/* 取得相片后返回本画面 */
startActivityForResult(intent, 1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
ContentResolver cr = this.getContentResolver();
try {
Bitmap bitmap = BitmapFactory.decodeStream(cr
.openInputStream(uri));
/* 将Bitmap设定到ImageView */
myImageView01.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}