简体   繁体   中英

Android - OnActivityResult Button Click

I have a fragment that have 4 buttons that call a camera. And i need to know that button that i have clicked to put the image into that button...

Can some one help me?

Here i call the image button click, check permissions and if all Ok, open the device camera...

img_first_veiculo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (permissionVerification.checkPermissionForCamera()) {
                if (permissionVerification.checkPermissionForExternalStorage()) {
                    openDeviceCamera();
                } else {
                    permissionVerification.requestPermissionForExternalStorage();
                }
            } else {
                permissionVerification.requestPermissionForCamera();
            }
        }
    });

OnActivityResult

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode != Activity.RESULT_OK) {
        return;
    }

    switch (requestCode) {

        case CROP_FROM_CAMERA: {

            //TODO Set image here
            Bitmap photo = (Bitmap) data.getExtras().get("data");
            img_first_veiculo.setImageBitmap(photo);

            break;
        }

        case PermissionVerification.CAMERA_PERMISSION_REQUEST_CODE: {

            Intent intent = new Intent("com.android.camera.action.CROP");

            intent.setDataAndType(mImageCaptureUri, "image/*");
            intent.putExtra("crop", "true");
            intent.putExtra("outputX", 640);
            intent.putExtra("outputY", 360);
            intent.putExtra("aspectX", 16);
            intent.putExtra("aspectY", 9);
            intent.putExtra("scale", true);
            intent.putExtra("return-data", true);
            startActivityForResult(intent, CROP_FROM_CAMERA);

            break;

        }
    }
}

As jeffery suggested you need to maintain a member varable mButtonclicked in your activity class ,in onclick remember whether its 1,2,3 or 4 and onacitivy result you can set it . For a beginner this should be fine but there is a chance that your actvity may be destroyed so read about onSavedinstance and onRestoreinstance

There's no magic. You know what button was pressed when you receive the "on click" event method call. You need to keep track of that yourself, such as in an instance field in your Activity subclass.

Don't forget to save the button pressed in instance state such that if your activity is destroyed while the camera is open, you will not lose the data.

Try to to save your data in onActivityResult and update Ui from onResume method.

or

activity.runOnUiThread(new Runnable() {
public void run() {
        Bitmap photo = (Bitmap) data.getExtras().get("data");
img_first_veiculo.setImageBitmap(photo);
}
});

我认为您首先需要在java类中定义按钮,使用更简单的名称命名按钮将使其更容易实现

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM