我应该如何获得相机和外部存储的android权限
作者:互联网
我在我的应用程序中有两个权限,它们基于访问相机和外部存储,但是我面临的问题是,在启动应用程序时仅要求相机权限,而没有要求其他权限.
但是在第二阶段,当我运行应用程序时,它请求了第二个权限.
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};
@Override
protected 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);
}
@Override
public 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) {
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
@Override
public 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之类的,
@Override
public 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-permissions,android 来源: https://codeday.me/bug/20191026/1935166.html