Android tutorial

android user permission – How to check Grants Permissions at Run-Time?

android user permission
1.2kviews

In this Android article, we are making android user permission. How to check android user permission is allowed or not in android. This is for marshmallow and higher and shows a dialog when permission is not allowed. In this Android tutorial, I am sharing How permission can be checked at runtime without throwing SecurityException? marshmallow and higher version and show a dialog to allow user permission.

Android user permission

Step 1: First one to  Start Android Studio

Step 2 :  Seconds step to Create a New Project Project Click On ==>  File  ==>NEW ==> New Project And Create Activity in your project 

And add permission in manifests file how you required like these:- 

Manifests

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="com.javapapers.android.androidlocationmaps.permission.MAPS_RECEIVE" />

Add these code where you can check user permission in any activity . for

Login.java  add these codes in your java class

public class LogIn extends AppCompatActivity {
    
    int PERMISSION_ALL = 1;
    String[] PERMISSIONS = {android.Manifest.permission.READ_EXTERNAL_STORAGE, android.Manifest.permission.WRITE_EXTERNAL_STORAGE,  android.Manifest.permission.CHANGE_NETWORK_STATE, android.Manifest.permission.CAMERA,android.Manifest.permission.ACCESS_FINE_LOCATION};

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

        if(!hasPermissions(this, PERMISSIONS)){
            ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
        }
       
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP){
        } else{
            // do something for phones running an SDK before lollipop
        }

    }

public static boolean hasPermissions(Context context, String... permissions) {
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
        for (String permission : permissions) {
            if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
    }
    return true;
} }

Also You can find the below check image permission for the android 13 and below. because of  above android 12 App permission is change.

public static String[] storage_permissions = {
        Manifest.permission.WRITE_EXTERNAL_STORAGE,
        Manifest.permission.READ_EXTERNAL_STORAGE
};

@RequiresApi(api = Build.VERSION_CODES.TIRAMISU)
public static String[] storage_permissions_33 = {
        Manifest.permission.READ_MEDIA_IMAGES,
};

public static String[] permissions() {
    String[] p;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
        p = storage_permissions_33;
    } else {
        p = storage_permissions;
    }
    return p;
}

private void pickImage() {
    if (Build.VERSION.SDK_INT >= 33 &&
            ContextCompat.checkSelfPermission(this, Manifest.permission.READ_MEDIA_IMAGES) != PackageManager.PERMISSION_GRANTED){
        ActivityCompat.requestPermissions(this, permissions(), 1);
    }else if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
        ActivityCompat.requestPermissions(this, permissions(), 1);
    }else {
        openGallery();
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == 1){
        openGallery();
    }
}

private void openGallery() {
    Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    resultLauncher.launch(galleryIntent);
}
ActivityResultLauncher<Intent> resultLauncher = registerForActivityResult(
        new ActivityResultContracts.StartActivityForResult(),
        new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {
                if (result.getResultCode() == Activity.RESULT_OK) {
                    // There are no request codes

                    Intent data = result.getData();
                    ImageURI = data != null ? data.getData() : null;
                    if (ImageURI != null) {
                        if (Build.VERSION.SDK_INT >= 28) {
                            ImageDecoder.Source source = ImageDecoder.createSource(getContentResolver(), ImageURI);
                            try {
                                if(ImageType.equals("profile")){
                                    profileImageBitmap = ImageDecoder.decodeBitmap(source);
                                    profile_Bill_photo.setImageBitmap(profileImageBitmap);
                                    uploadImage("profileImage");
                                }else if(ImageType.equals("doc")){
                                    docImageBitmap = ImageDecoder.decodeBitmap(source);
                                    docr_photo.setImageBitmap(docImageBitmap);
                                    uploadImage("docimage");
                                }else {

                                }

                            } catch (IOException e) {
                                throw new RuntimeException(e);
                            }

                        } else {
                            try {
                                if(ImageType.equals("profile")){
                                    profileImageBitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), ImageURI);
                                    profile_Bill_photo.setImageBitmap(profileImageBitmap);
                                    uploadImage("profileimage");
                                }else if(ImageType.equals("doc")){
                                    docImageBitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), ImageURI);
                                    doc_photo.setImageBitmap(docImageBitmap);
                                    uploadImage("docimage");
                                }else {

                                }
                            } catch (IOException e) {
                                throw new RuntimeException(e);
                            }

                        }
                    }
                }
            }
        });

 

 

Read More Tutorial 

Welcome to my blog! I’m Ritu Malik, and here at Codeplayon.com, we are dedicated to delivering timely and well-researched content. Our passion for knowledge shines through in the diverse range of topics we cover. Over the years, we have explored various niches such as business, finance, technology, marketing, lifestyle, website reviews and many others.