ernestoyaquello / VerticalStepperForm

Vertical Stepper Form Library for Android. It follows Google Material Design guidelines.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Capture image from camera

abarazal opened this issue · comments

Hi! I have the code below that I want to take to a Step in order to capture image from camera. Since Step is not an Activity, how can I access to checkSelfPermission, requestPermissions and startActivityForResult methods from within a class that extends from Step?

public class MyCameraActivity extends Activity {
	
    private static final int CAMERA_REQUEST = 1888; 
    private ImageView imageView;
    private static final int MY_CAMERA_PERMISSION_CODE = 100;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        this.imageView = (ImageView)this.findViewById(R.id.imageView1);
        Button photoButton = (Button) this.findViewById(R.id.button1);
        photoButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                    requestPermissions(new String[]{Manifest.permission.CAMERA}, MY_CAMERA_PERMISSION_CODE);
                }
                else {
                    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
                    startActivityForResult(cameraIntent, CAMERA_REQUEST);
                } 
            }
        });
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == MY_CAMERA_PERMISSION_CODE) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this, "camera permission granted", Toast.LENGTH_LONG).show();
                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
                startActivityForResult(cameraIntent, CAMERA_REQUEST);
            }
            else {
                Toast.makeText(this, "camera permission denied", Toast.LENGTH_LONG).show();
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
        if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {  
            Bitmap photo = (Bitmap) data.getExtras().get("data"); 
            imageView.setImageBitmap(photo);
        }  
    } 
}

To get this implemented, you would want to keep the callbacks for onRequestPermissionsResult and onActivityResult on your activity or fragment, just as your code shows.

Then, you would want to be able to invoke the methods of your activity from your step. There are many ways to achieve that, but judging by your code, a good one would be to pass the click listener to your step when constructing it:

public class YourStep extends Step<YourStepType> {

    private View.OnClickListener cameraButtonClickListener;

    public YourStep(String title, View.OnClickListener cameraButtonClickListener) {
        super(title);
        this.cameraButtonClickListener = cameraButtonClickListener;
    }

    @NonNull
    @Override
    protected View createStepContentLayout() {
        LayoutInflater inflater = LayoutInflater.from(getContext());
        View stepContent = inflater.inflate(R.layout.your_step_layout, null, false);
        Button cameraButton = stepContent.findViewById(R.id.camera_button);
        cameraButton.setOnClickListener(cameraButtonClickListener);
        return stepContent;
    }

    ...
}

This way, you could create the step from your activity:

YourStep yourStep = new YourStep("Your Step Title", new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{Manifest.permission.CAMERA}, MY_CAMERA_PERMISSION_CODE);
        }
        else {
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, CAMERA_REQUEST);
        }
    }
});

// Now you can set up the form with your step
...

I hope this helps!