1

I need to ask the user for external storage when the app is installed for the first time. The code which I have used asks for the camera permission but not for the storage permissions. How do I solve it?

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);
            }
        });
    }
}
artplastika
  • 1,972
  • 2
  • 19
  • 38

2 Answers2

0

you overwriting your permissions your READ_EXTERNAL_STORAGE permission overlay your previous permission, try this permissions array

declear it before oncreate and replace with your permissions

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

if permission allowed check and do your work here

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

            }

And here is check permission method

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;
    }

let me know if it works for you.

Sandeep Parish
  • 1,888
  • 2
  • 9
  • 20
0

First, no need to check for individual permissions...you can check multiple permissions together.

Check for multiple permissions

By this way it will solve your problem too

CreSoul
  • 16
  • 5