具有不同意图的onActivityResult?

时间:2017-12-13 05:53:08

标签: android onactivityresult

我有一个应用程序,我想使用两个按钮,一个用于打开相机,另一个应该打开图库并选择一个图像。

创建onActivityResult的正确方法是什么?我从相机开始,它的工作原理。然后我添加了具有意图的选择来自图库按钮,但第一个“if”-clause始终被触发,因为bec REQUEST_TAKE_PHOTO始终为真/ 1。在所有的例子中,我发现他们总是使用最终的静态int来完成这些任务。 在触发onActivityState之前,我是否应该使用最终的静态int并将它们设置为正确的状态? 人们通常如何解决这个问题?

   static final int REQUEST_TAKE_PHOTO = 1;
    static final int PICK_IMAGE_REQUEST = 1;
    ...
    cameraButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // Ensure that there's a camera activity to handle the intent
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            // Create the File where the photo should go
            File photoFile = null;
            try {
                photoFile = createImageFile();
            } catch (IOException ex) {
                // Error occurred while creating the File
                ex.printStackTrace();
            }
            // Continue only if the File was successfully created
            if (photoFile != null) {
                Uri photoURI = FileProvider.getUriForFile(this,
                        "com.example.fileprovider",
                        photoFile);
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
            }
        }
    }
            }
        });
galleryButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("image/*");

                startActivityForResult(Intent.createChooser(intent, "Välj Bild"), PICK_IMAGE_REQUEST);
            }
        });
 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        //super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && data != null) {

            if (requestCode == REQUEST_TAKE_PHOTO) {
                try {
                    setPic();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else if (requestCode == PICK_IMAGE_REQUEST && data.getData() != null) {
                Uri uri = data.getData();
                mImageView.setImageURI(uri);

            }
        } else {
            Log.d("Error", "Error");
        }
    }

2 个答案:

答案 0 :(得分:4)

为什么两个请求都使用相同的请求代码。改变这些。

 static final int REQUEST_TAKE_PHOTO = 1;
static final int PICK_IMAGE_REQUEST = 1;

请求代码应该彼此唯一,所以请使用。

 static final int REQUEST_TAKE_PHOTO = 1;
static final int PICK_IMAGE_REQUEST = 2;

onActivityresult()可以如下所示。

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_TAKE_PHOTO) {
        if (resultCode == RESULT_OK) {
            // camera picture
        } else {
            // request canceled
        }
    } else if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK) {
        if (data != null) {
            Uri uri = data.getData();
            mImageView.setImageURI(uri);
        }
    }
}

答案 1 :(得分:2)

您可以在 OnActivityResult 中使用switch语句,如下所示:

你需要改变你的int值,如下所示:

static final int REQUEST_TAKE_PHOTO = 1;
static final int PICK_IMAGE_REQUEST = 2;


 @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    try {
        switch (requestCode) {
            case REQUEST_TAKE_PHOTO :
              /// your code for this 
            break;
            case PICK_IMAGE_REQUEST :
             // your code here for image request.
            break;
}
}catch (Exception e) {
}