`

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

 

0
6
分享到:
评论
3 楼 ITeye管理员 2012-09-25  
白云天 写道
BBCode编辑器
怎样用?

您就发文章的时候选择BBCode编辑器,不要选择可视化编辑就好。我会给你发站短,附上截图,请注意查收。
2 楼 白云天 2012-09-25  
BBCode编辑器
怎样用?
1 楼 ITeye管理员 2012-09-25  
强烈建议您使用BBCode编辑器,将文中的代码部分排版工整。

相关推荐

Global site tag (gtag.js) - Google Analytics