在Activity之间传递数据

在一个Activity中启动另一个Activy,取得数据,如:在应用 A 中启动 通讯录,选择目标用户,返回,以便在A 中使用选择的通讯录数据,基本过程如下:

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

在A 中加入下列代码:

 @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);
    }
  }

猜你喜欢

转载自iwin.iteye.com/blog/1684891