这个方法的意义是什么?

时间:2014-04-06 23:56:37

标签: android

这是我的全部代码:

public class photo extends Activity implements View.OnClickListener {

ImageView iv;
Button bt;
ImageButton ib;
Intent i ;
Bitmap bmp;
final static int cameraData =0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    intsallttion();

}
private void intsallttion() {
    // TODO Auto-generated method stub
    iv = (ImageView) findViewById(R.id.iv);
    bt = (Button) findViewById(R.id.bt);
    ib = (ImageButton) findViewById(R.id.ib);

    bt.setOnClickListener(this);
    ib.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    switch(v.getId()){
    case R.id.bt:

        break;

    case R.id.ib:
        i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(i,cameraData);
        break;
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode==RESULT_OK){
        Bundle extras = data.getExtras();
        bmp = (Bitmap) extras.get("data");
        iv.setImageBitmap(bmp);
    }
}
}

我不明白这种方法:

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode==RESULT_OK){
        Bundle extras = data.getExtras();
        bmp = (Bitmap) extras.get("data");
        iv.setImageBitmap(bmp);
    }
}

这种方法有什么作用?

3 个答案:

答案 0 :(得分:1)

根据您的代码,您正在调用相机来捕获图像。因为您使用startActivityForResult启动了相机活动,所以您希望从相机活动中获得一些结果。

当来自活动的图像被捕获时,将使用捕获的结果(图像)调用startActivityForResult。

答案 1 :(得分:1)

startActivityForResult调用android的内部意图(在你的情况下,它是打开相机,用户可以拍照)。

拍摄照片后,它会将控件返回到onActivityResult处理程序,您可以在其中检查resultCode是否为RESULT_OK并采取必要的操作,例如在ImageView中设置图像使用iv.setImageBitmap(bmp);

答案 2 :(得分:0)

你通过startActivityForResult打开了相机的活动,然后正如利宾对你说的那样,你在那里得到了结果;结果是图片。我试图添加一些内容:

     @Override
     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     // TODO Auto-generated method stub
      super.onActivityResult(requestCode, resultCode, data);
      // you check the result code that you got from the activity of the camera
      // If the user took successfully a picture you get RESULT_OK else RESULT_CANCELED
      if(resultCode==RESULT_OK){
           // The picture is communicated from the activity of the camera to your activity 
           // by intent as an extras Bundle : you put this bundle in bundle
           Bundle extras = data.getExtras();
           // you take the bitmap (your picture is in format bitmap)form this extras 
           bmp = (Bitmap) extras.get("data");
           // you are showing the bitmap in the ImageView iv
           iv.setImageBitmap(bmp);
    }
}