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方法,它將響應(yīng)choosePicture按鈕的單擊事件,該按鈕在應(yīng)用程序啟動時顯示,如圖3-1所示。這個方法創(chuàng)建意圖,其將觸發(fā)Gallery應(yīng)用程序,使其以允許用戶選擇一張圖片的模式啟動,如圖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);