---
title: "Code for check storage permission in android."  
description: "Code for check storage permission in android."  
author: "Arti Mishra"  
published: 2018-08-23  
updated: 2023-07-02  
canonical: https://www.mindstick.com/forum/34667/code-for-check-storage-permission-in-android  
category: "android apps"  
tags: ["android"]  
reading_time: 2 minutes  

---

# Code for check storage permission in android.

[Code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) for [check](https://yourviews.mindstick.com/story/2248/never-forget-to-check-these-specifications-before-buying-a-mobile-phone) [storage](https://www.mindstick.com/articles/44536/finding-your-self-storage-facility) [permission](https://www.mindstick.com/forum/159434/linux-service-permission-denied-error-adjust-permissions) in android.

## Replies

### Reply by Aryan Kumar

Sure, here is the code to check storage permission in Android:

public boolean checkStoragePermission() { // Check if the permission has already been granted int permissionStatus = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);

Code snippet

```plaintext
// If the permission has not been granted, request it
if (permissionStatus != PackageManager.PERMISSION_GRANTED) {
    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 100);
    return false;
} else {
    // The permission has already been granted
    return true;
}
```

}

This code will first check if the `WRITE_EXTERNAL_STORAGE` permission has already been granted. If it has not been granted, the code will request the permission from the user. If the user grants the permission, the code will return `true`. Otherwise, the code will return `false`.

To use this code, you would need to call the `checkStoragePermission()` method in your app. For example, you could call the method in your app's `onCreate()` method.

Here is an example of how you could call the `checkStoragePermission()` method in your app's `onCreate()` method:

public void onCreate() { super.onCreate();

Code snippet

```plaintext
// Check if the storage permission has been granted
if (checkStoragePermission()) {
    // The permission has already been granted
} else {
    // Request the permission
    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 100);
}
```

### Reply by Arti Mishra

## Code for storage permission

public void checkForStoragePermission(){

if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {

## // Permission is not granted

## // Should we show an explanation

if (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.

ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},

MY_PERMISSIONS_REQUEST_READ_STORAGE);

}

} else {

Toast.makeText(this, "Permission granted", Toast.LENGTH_SHORT).show();

}

if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {

// Permission is not granted

Toast.makeText(this, "Permission needed to read images", Toast.LENGTH_LONG).show();

}

}

## \


---

Original Source: https://www.mindstick.com/forum/34667/code-for-check-storage-permission-in-android

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
