我在我的应用程序中有两个权限,它们基于访问相机和外部存储,但是我面临的问题是,在启动应用程序时仅要求相机权限,而没有要求其他权限.
但是在第二阶段,当我运行应用程序时,它请求了第二个权限.
public class MainActivity extends AppCompatActivity {private DrawerLayout mDrawer;private Toolbar toolbar;private NavigationVIEw nvDrawer;private ActionbarDrawerToggle drawerToggle;private static final int REQUEST_CAMERA = 0;private static final int REQUEST_EXTERNAL_STORAGE = 1;private static String[] PERMISSION_EXTERNAL = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE};@OverrIDeprotected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) { // Show an explanation to the user *asynchronously* -- don't block // this thread waiting for the user's response! After the user // sees the explanation, try again to request the permission. } else { ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA); } } else if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) || ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_EXTERNAL_STORAGE)) { // Show an explanation to the user *asynchronously* -- don't block // this thread waiting for the user's response! After the user // sees the explanation, try again to request the permission. } else { ActivityCompat.requestPermissions(MainActivity.this, PERMISSION_EXTERNAL, REQUEST_EXTERNAL_STORAGE); } } setContentVIEw(R.layout.activity_main); toolbar = (Toolbar) findVIEwByID(R.ID.toolbar); setSupportActionbar(toolbar); nvDrawer = (NavigationVIEw) findVIEwByID(R.ID.nvVIEw); mDrawer = (DrawerLayout) findVIEwByID(R.ID.drawer_layout); setupDrawerContent(nvDrawer); drawerToggle = setupDrawerToggle(); mDrawer.addDrawerListener(drawerToggle);}@OverrIDepublic voID onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (requestCode == REQUEST_CAMERA) { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { //granted } else { ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA); } } else if (requestCode != REQUEST_EXTERNAL_STORAGE) { if (PermissionUtil.verifyPermission(grantResults)) { //Has been granted } else { //Not granted for permission } } else { super.onRequestPermissionsResult(requestCode, permissions, grantResults); }}private ActionbarDrawerToggle setupDrawerToggle() { return new ActionbarDrawerToggle(this, mDrawer, toolbar, R.string.drawer_open, R.string.drawer_close);}private voID setupDrawerContent(NavigationVIEw navigationVIEw) { navigationVIEw.setNavigationItemSelectedListener( new NavigationVIEw.OnNavigationItemSelectedListener() { @OverrIDe public boolean onNavigationItemSelected(MenuItem menuItem) { selectDrawerItem(menuItem); return true; } });}private voID selectDrawerItem(MenuItem menuItem) {}@OverrIDeprotected voID onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); drawerToggle.syncState();}@OverrIDepublic voID onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); drawerToggle.onConfigurationChanged(newConfig);}@OverrIDepublic boolean onoptionsItemSelected(MenuItem item) { if (drawerToggle.onoptionsItemSelected(item)) { return true; } return super.onoptionsItemSelected(item);}
}
实用程序类,用于检查androID上的多个权限以进行读取和写入外部权限
public abstract class PermissionUtil {public static boolean verifyPermission(int[] grantResults){ // At least one result must be checked. if (grantResults.length > 0){ return false; } for (int results :grantResults){ if (results != PackageManager.PERMISSION_GRANTED){ return false; } } return true;}
}
甚至我都允许清单中的所有人.
解决方法:
您可以将多个权限添加到列表中.
检查多个权限的方法
public static final int REQUEST_ID_MulTIPLE_PERMISSIONS = 101;public static boolean checkAndRequestPermissions(final Activity context) { int ExtstorePermission = ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE); int cameraPermission = ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA); List<String> ListPermissionsNeeded = new ArrayList<>(); if (cameraPermission != PackageManager.PERMISSION_GRANTED) { ListPermissionsNeeded.add(Manifest.permission.CAMERA); } if (WExtstorePermission != PackageManager.PERMISSION_GRANTED) { ListPermissionsNeeded .add(Manifest.permission.WRITE_EXTERNAL_STORAGE); } if (!ListPermissionsNeeded.isEmpty()) { ActivityCompat.requestPermissions(context, ListPermissionsNeeded .toArray(new String[ListPermissionsNeeded.size()]), REQUEST_ID_MulTIPLE_PERMISSIONS); return false; } return true;}
像这样使用
if(checkAndRequestPermissions(MainActivity.this)){ doWork();}
并处理PermissionsResult之类的,
@OverrIDepublic voID onRequestPermissionsResult(int requestCode,String[] permissions, int[] grantResults) { switch (requestCode) { case Utility.REQUEST_ID_MulTIPLE_PERMISSIONS: if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { Toast.makeText(getApplicationContext(), "FlagUp Requires Access to Camara.", Toast.LENGTH_SHORT) .show(); finish(); } else if (ContextCompat.checkSelfPermission(Splash_Activity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { Toast.makeText(getApplicationContext(), "FlagUp Requires Access to Your Storage.", Toast.LENGTH_SHORT).show(); finish(); } else { doWork(); } break; }}
快乐编码
总结以上是内存溢出为你收集整理的我应该如何获得相机和外部存储的android权限全部内容,希望文章能够帮你解决我应该如何获得相机和外部存储的android权限所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)