如何合并两个onActivityResult?

时间:2015-12-13 18:58:08

标签: android onactivityresult

我在做在线申请。其中一个功能是用户可以从他们的图库中选择照片或者从他们的相机拍照。现在我在onActivityResult方法中遇到了麻烦,我无法合并它。希望有人可以帮我合并它以便用户选择或拍摄的图像可以在我的ImageView(image_view)中查看。这是我的主要活动:

**private static final int REQUEST_CODE = 1;
private Button button_1;
public int TAKE_PICTURE = 1;
private ImageView image_view;
private Bitmap bitmap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sell);

    image_view = (ImageView) findViewById(R.id.resul);
    button_1 = (Button) findViewById(R.id.button4);

    button_1.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {

            Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
            startActivityForResult(intent, TAKE_PICTURE);
        }
    });

    Button buttonLoadImage = (Button) findViewById(R.id.button3);
    buttonLoadImage.setOnClickListener(new Button.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(intent, 0);
        }
    });

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)
        try {
            // We need to recyle unused bitmaps
            if (bitmap != null) {
                bitmap.recycle();
            }
            InputStream stream = getContentResolver().openInputStream(data.getData());
            bitmap = BitmapFactory.decodeStream(stream);
            stream.close();
            image_view.setImageBitmap(bitmap);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    super.onActivityResult(requestCode, resultCode, data);
}

@Override
protected void onActivityResult2(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == RESULT_OK) {
        Uri targetUri = data.getData();
        Bitmap bitmap;
        try {
            bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri));
            image_view.setImageBitmap(bitmap);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

在这里输入代码

1 个答案:

答案 0 :(得分:1)

您不需要为两个onActivityResult()调用创建两个startActivityForResult方法。只需在startActivityForResult调用中提供不同的请求代码即可。并且onActivityResult会通过给定的请求代码通知您,以确定结果。

private static final int TAKE_PICTURE = 100;
private static final int CHOOSE_PICTURE = 101;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sell);

    image_view = (ImageView) findViewById(R.id.resul);
    button_1 = (Button) findViewById(R.id.button4);

    button_1.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {

            Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
            // Here provide TAKE_PICTURE request code to identify the IMAGE_CAPTURE result in onActivityResult method. 
            startActivityForResult(intent, TAKE_PICTURE);
        }
    });

    Button buttonLoadImage = (Button) findViewById(R.id.button3);
    buttonLoadImage.setOnClickListener(new Button.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(Intent.ACTION_PICK,
            // Here provide CHOOSE_PICTURE request code to identify the ACTION_PICK result in onActivityResult method.     android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(intent, CHOOSE_PICTURE);
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == TAKE_PICTURE && resultCode == Activity.RESULT_OK)
            // Do something with take picker result
    }else if (requestCode == CHOOSE_PICTURE && resultCode == Activity.RESULT_OK)
            // Do something with choose image result
    }
}