5

I am currently learning android development via the course provided by MeiCode. So far the course has been excellent and I am really getting it. However, I have come to a point where the course is a little outdated. I following a tutorial where he shows how to request camera permissions and then use the camera to snap a photo. However, when I type the code to open the camera via an intent and the startActivityForResult method, Android Studio informs me that it is deprecated and that I should use registerForActivityResult. This wouldnt be a big deal if not for the fact that I am a newbie and the documentation for the method isn't exactly newbie friendly imo. And I couldnt find any videos covering the method in Java (all of them show how to use it in Kotlin). So I am here asking if someone can refer to me an easy to follow guide or video, or do an ELI5 explanation of how to use the method for my purpose. Here is the code I have written so far:

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private static final int CAMERA_PERMISSION_CODE = 101;
    private static final int CAMERA_INTENT_RESULT_CODE = 102;

    private Button button;
    private ImageView image;
    private RelativeLayout parent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = findViewById(R.id.button);
        image = findViewById(R.id.image);
        parent = findViewById(R.id.parent);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                handlePermission();
            }
        });


    }

    private void handlePermission() {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
//            openCamera();
            Toast.makeText(this, "You have permission to use the camera!", Toast.LENGTH_SHORT).show();
        } else {
            if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
                showSnackBar();
            } else {
                ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.CAMERA}, CAMERA_PERMISSION_CODE);
            }
        }
    }

    private void showSnackBar() {
    }

    private void openCamera() {
        Intent intent =  new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, CAMERA_INTENT_RESULT_CODE);
    }
}

How can I use the new method to accomplish this task instead of the deprecated method? All help is much appreciated.

Zain
  • 37,492
  • 7
  • 60
  • 84
Chris Ray
  • 133
  • 1
  • 2
  • 8
  • 4
    https://stackoverflow.com/questions/62671106/onactivityresult-method-is-deprecated-what-is-the-alternative This shall help you. – che10 Jun 15 '21 at 17:38
  • @che10 Thanks for pointing me to this article! You are a gentleman and a scholar! – Chris Ray Jun 15 '21 at 18:12
  • Does this answer your question? [OnActivityResult method is deprecated, what is the alternative?](https://stackoverflow.com/questions/62671106/onactivityresult-method-is-deprecated-what-is-the-alternative) – Zain Jun 15 '21 at 21:11

0 Answers0