最简便打开相册方法

这样打开相册

 initPrediction();
 Intent intent = new Intent();
 /* 开启Pictures画面Type设定为image */
  intent.setType("image/*");
  /* 使用Intent.ACTION_GET_CONTENT这个Action */
  intent.setAction(Intent.ACTION_GET_CONTENT);
  /* 取得相片后返回本画面 */
  startActivityForResult(intent, 1);

initPrediction方法

private void initPrediction() {
    Album.setEnabled(true);
    }

onActivityResult方法

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            Uri uri = data.getData();
            Log.e("uri", uri.toString());
            ContentResolver cr = this.getContentResolver();
            try {
                Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));
                ImageView imageView = (ImageView) findViewById(R.id.photo);
                /* 将Bitmap设定到ImageView */
                imageView.setImageBitmap(bitmap);


            } catch (FileNotFoundException e) {
                Log.e("Exception", e.getMessage(),e);
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

你可能感兴趣的:(app)