Storage Permission in Android 13

 

In this comprehensive tutorial, we'll show you how to effectively manage multiple permissions in Android 13, ensuring a seamless user experience and airtight security. If you're an Android app developer looking to navigate the complexities of handling various permissions, you're in the right place!

📱 Topics Covered:

  1. Requesting Permissions in Android 13
  2. Checking for Permission Status
  3. Handling Multiple Permission Requests
  4. Best Practices for Permission Management
  5. Implementing Permission Rationale
  6. Managing Permission Callbacks
  7. Streamlining User Experience
  8. Security Considerations
  9. Compatibility with Android 13

All Codes :

 public class MainActivity extends AppCompatActivity {  
   
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_main);  
   
     findViewById(R.id.userPermission).setOnClickListener(view -> {  
       myPermissions();  
     });  
   
   
   } // onCreate method end here ==================  
   
   private final ActivityResultLauncher<String[]> requestPermissionLauncher =  
       registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), permission ->{  
         boolean allGranted = true;  
   
         for (Boolean isGranted : permission.values()){  
           if (!isGranted){  
             allGranted = false;  
             break;  
           }  
         }  
   
         if (allGranted){  
           // All is granted  
         } else {  
           // All is not granted  
         }  
   
       });  
   
   private void myPermissions(){  
   
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU){  
   
       String[] permissions = new String[]{  
           android.Manifest.permission.READ_MEDIA_IMAGES,  
           android.Manifest.permission.READ_MEDIA_AUDIO,  
           android.Manifest.permission.READ_MEDIA_VIDEO,  
           android.Manifest.permission.CAMERA,  
       };  
   
   
       List<String> permissionsTORequest = new ArrayList<>();  
       for (String permission : permissions){  
         if (ContextCompat.checkSelfPermission(this,permission) != PackageManager.PERMISSION_GRANTED){  
           permissionsTORequest.add(permission);  
         }  
       }  
   
       if (permissionsTORequest.isEmpty()){  
         // All permissions are already granted  
         Toast.makeText(this, "All permissions are already granted", Toast.LENGTH_SHORT).show();  
   
   
       } else {  
         String[] permissionsArray = permissionsTORequest.toArray(new String[0]);  
         boolean shouldShowRationale = false;  
   
         for (String permission : permissionsArray){  
           if (shouldShowRequestPermissionRationale(permission)){  
             shouldShowRationale = true;  
             break;  
           }  
         }  
   
         if (shouldShowRationale){  
           new AlertDialog.Builder(this)  
               .setMessage("Please allow all permissions")  
               .setCancelable(false)  
               .setPositiveButton("YES", new DialogInterface.OnClickListener() {  
                 @Override  
                 public void onClick(DialogInterface dialogInterface, int i) {  
                   requestPermissionLauncher.launch(permissionsArray);  
                 }  
               })  
   
               .setNegativeButton("NO", new DialogInterface.OnClickListener() {  
                 @Override  
                 public void onClick(DialogInterface dialogInterface, int i) {  
                   dialogInterface.dismiss();  
                 }  
               })  
               .show();  
   
         } else {  
           requestPermissionLauncher.launch(permissionsArray);  
         }  
   
   
       }  
   
   
     } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {  
       String[] permissions = new String[]{  
           android.Manifest.permission.READ_EXTERNAL_STORAGE,  
       };  
   
   
       List<String> permissionsTORequest = new ArrayList<>();  
       for (String permission : permissions){  
         if (ContextCompat.checkSelfPermission(this,permission) != PackageManager.PERMISSION_GRANTED){  
           permissionsTORequest.add(permission);  
         }  
       }  
   
       if (permissionsTORequest.isEmpty()){  
         // All permissions are already granted  
         Toast.makeText(this, "All permissions are already granted", Toast.LENGTH_SHORT).show();  
   
   
       } else {  
         String[] permissionsArray = permissionsTORequest.toArray(new String[0]);  
         boolean shouldShowRationale = false;  
   
         for (String permission : permissionsArray){  
           if (shouldShowRequestPermissionRationale(permission)){  
             shouldShowRationale = true;  
             break;  
           }  
         }  
   
         if (shouldShowRationale){  
           new AlertDialog.Builder(this)  
               .setMessage("Please allow all permissions")  
               .setCancelable(false)  
               .setPositiveButton("YES", new DialogInterface.OnClickListener() {  
                 @Override  
                 public void onClick(DialogInterface dialogInterface, int i) {  
                   requestPermissionLauncher.launch(permissionsArray);  
                 }  
               })  
   
               .setNegativeButton("NO", new DialogInterface.OnClickListener() {  
                 @Override  
                 public void onClick(DialogInterface dialogInterface, int i) {  
                   dialogInterface.dismiss();  
                 }  
               })  
               .show();  
   
         } else {  
           requestPermissionLauncher.launch(permissionsArray);  
         }  
   
   
       }  
   
   
     }  
   
   
   } // myPermissions end here ================  
   
   
 } // public class end here =========================  

Video :




Next Post Previous Post
No Comment
Add Comment
comment url