Android app can't read external storage files

后端 未结 5 563
遇见更好的自我
遇见更好的自我 2020-12-19 18:52

I\'m creating an app for a private use only... it\'s not so important then, respect your time :P

What I want to make is an app that reads from a database and plays s

5条回答
  •  佛祖请我去吃肉
    2020-12-19 19:17

    if your targetSdk is 23 than you have to check permissions like this for marshmallow and above devices

    if (Build.VERSION.SDK_INT == Build.VERSION_CODES.M) {
        checkPermission();
    }
    else {
    }
    
    
    private void checkPermission() {
            if (ContextCompat.checkSelfPermission(this,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this,
                Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {//Can add more as per requirement
    
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE},
                        123);
    
            } else {
    
            }
    }
    
    @Override
    public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults)   
       {
        switch (requestCode) {
            case 123: {
    
    
     if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED)     {
       //Peform your task here if any
        } else {
    
            checkPermission();
        }
            return;
        }
    }
    }
    

提交回复
热议问题