使用ONActivityResult()

时间:2013-02-15 21:57:30

标签: android android-intent android-activity

我在OnClickListener()上使用了多个按钮,点击了startActivityForResult()。在OnActivityResult()方法中,有不同的操作要执行。如何获得正确的按钮以获得正确的结果?

    @Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
case R.id.camImgButton:
        i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(i, picture);
        break;

    case R.id.galImgButton:
        i = new Intent();
        i.setType("image/*");
        i.setAction(Intent.ACTION_GET_CONTENT);
        i.addCategory(Intent.CATEGORY_OPENABLE);
        startActivityForResult(i, REQUEST_CODE);
        break;
    case R.id.txtButton:

    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    switch (       ) {
    case        :

    if (resultCode == RESULT_OK) {
        // We need to recyle unused bitmaps
        if (bmp != null) {
            bmp.recycle();
        }
        Bundle extras = data.getExtras();
        bmp = (Bitmap) extras.get("data");
        display.setImageBitmap(bmp);
        break;
        case        :

        InputStream stream = null;
    if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)
        try {
        // We need to recyle unused bitmaps
        if (bmp != null) {
        bmp.recycle();
        }
    stream = getContentResolver().openInputStream(data.getData());
    bmp = BitmapFactory.decodeStream(stream);

                    display.setImageBitmap(bmp);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } finally {
                    if (stream != null)
                        try {
                            stream.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                }
        }
    }

1 个答案:

答案 0 :(得分:1)

您可以针对每个按钮的不同请求代码启动结果活动,并在OnActivityResult方法中检查发回的requestCode并将其与您想要的操作相匹配,我认为您已经在代码中使用了