public class ChoosePicture extends Activity implements OnClickListener {
ImageView chosenImageView;
Button choosePicture;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
chosenImageView = (ImageView) this.findViewById(R.id.ChosenImageView);
choosePicture = (Button) this.findViewById(R.id.ChoosePictureButton);
choosePicture.setOnClickListener(this);
}
随后是onClick方法,它将响应choosePicture按钮的单击事件,该按钮在应用程序启动时显示,如图3-1所示。这个方法创建意图,其将触发Gallery应用程序,使其以允许用户选择一张图片的模式启动,如图3-2所示。
public void onClick(View v) {
Intent choosePictureIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(choosePictureIntent, 0);