1

当单击 startActivityForResult() 时,我正在使用带有 OnClickListener() 的多个按钮。在 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();
                        }
                }
        }
    }
4

1 回答 1

1

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

于 2013-02-15T22:03:16.330 回答