Saturday, 5 May 2018

How to give permission programatically in android


- To call isStoragePermissionGranted method in OnCreate()

public boolean isStoragePermissionGranted() {

if (Build.VERSION.SDK_INT >= 23) {            
 if(checkSelfPermission(android.Manifest.permission.
WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED{   
    Log.v("PDF", "Permission is granted");      
    return true;            
} else {           
    Log.v("PDF", "Permission is revoked");            
    ActivityCompat.requestPermissions(this, new String[]  {Manifest.permission.WRITE_EXTERNAL_STORAGE},1); 
    return false;            
  }       
} else {
    //permission is automatically granted on sdk<23 upon installation            
    Log.v("PDF", "Permission is granted");     
    return true;      
  }   
}


No comments:

Post a Comment