READ_EXTERNAL_STORAGE权限不要求许可

时间:2018-06-02 09:20:44

标签: android

首次安装应用时,我需要让用户提供外部存储空间。我使用的代码请求相机权限,但不是存储权限。我该如何解决?

public class MainActivity extends AppCompatActivity {

    Button loginbutton;

    @TargetApi(Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{Manifest.permission.CAMERA}, 1888);
        }
        if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},211);
        }
        loginbutton = (Button)findViewById(R.id.loginButton);
        loginbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext() , Home.class);
                startActivity(intent);
            }
        });
    }
}

2 个答案:

答案 0 :(得分:0)

如果您的READ_EXTERNAL_STORAGE权限覆盖了之前的权限,则会覆盖您的权限,请尝试使用此权限数组

在oncreate之前解除它并用你的权限替换

String[] permissions = new String[]{
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.CAMERA,
            Manifest.permission.RECORD_AUDIO};

如果允许检查并在此处开展工作

if (checkPermissions()) {
           //your work if permission allowed

            }

这是检查许可方法

private  boolean checkPermissions() {
        int result;
        List<String> listPermissionsNeeded = new ArrayList<>();
        for (String p:permissions) {
            result = ContextCompat.checkSelfPermission(this,p);
            if (result != PackageManager.PERMISSION_GRANTED) {
                listPermissionsNeeded.add(p);
            }
        }
        if (!listPermissionsNeeded.isEmpty()) {
            ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]),MULTIPLE_PERMISSIONS );
            return false;
        }
        return true;
    }

让我知道它是否适合你。

答案 1 :(得分:0)

首先,无需检查个人权限......您可以一起检查多个权限。

Check for multiple permissions

通过这种方式,它也将解决您的问题