Detect Check SD Card is present or not in android with Runtime permission

How to know external storage memory card is available or not in android device with Marshmallow permission support.

Memory card also known as external storage is very import for android mobile phones because it gives the facility to their users to increase the storage capacity of mobile device. So in this tutorial we are going to Detect Check SD Card is present or not in android. The access of external storage memory need WRITE_EXTERNAL_STORAGE permission so for our users we will also implementing Marshmallow run time permission request method hence this tutorial will work on any android platform.

android-project-download-code-button

How to Detect Check SD Card is present or not in android with Runtime permission.

Code for MainActivity.java file.

package com.android_examples.detectsdcard_android_examplescom;
import android.Manifest;
import android.content.pm.PackageManager;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.support.v4.app.ActivityCompat;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    Button button ;
    TextView textView;
    Boolean SDcard ;
    public  static final int RequestPermissionCode  = 1 ;
    int RequestCheckResult  ;
    boolean RequestTF ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button)findViewById(R.id.button);
        textView = (TextView)findViewById(R.id.textView);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                PermissionStatus();

                if(RequestTF){

                    SDCardCheck();
                }
                else {

                    EnableRuntimePermission();
                }

            }
        });
    }

    public void SDCardCheck(){

        SDcard = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);

        if(SDcard)
        {
            textView.setText("SD CARD IS AVAILABLE");
        }
        else
        {
            textView.setText("SD CARD NOT AVAILABLE");
        }
    }

    public void PermissionStatus(){

        RequestCheckResult = ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE);

        if (RequestCheckResult == PackageManager.PERMISSION_GRANTED){

            RequestTF = true;

        } else {

            RequestTF = false;

        }

    }

    public void EnableRuntimePermission(){

        if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE))
        {

            Toast.makeText(MainActivity.this,"WRITE_EXTERNAL_STORAGE permission allows us to Access SD CARD app", Toast.LENGTH_LONG).show();

        } else {

            ActivityCompat.requestPermissions(MainActivity.this,new String[]{
                    Manifest.permission.WRITE_EXTERNAL_STORAGE}, RequestPermissionCode);

        }
    }

    @Override
    public void onRequestPermissionsResult(int RC, String per[], int[] PResult) {

        switch (RC) {

            case RequestPermissionCode:

                if (PResult.length > 0 && PResult[0] == PackageManager.PERMISSION_GRANTED) {

                    Toast.makeText(MainActivity.this,"Permission Granted, Now your application can access WRITE_EXTERNAL_STORAGE.", Toast.LENGTH_LONG).show();

                } else {

                    Toast.makeText(MainActivity.this,"Permission Canceled, Now your application cannot access WRITE_EXTERNAL_STORAGE.", Toast.LENGTH_LONG).show();

                }
                break;
        }
    }

}

Code for activity_main.xml layout file.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.android_examples.detectsdcard_android_examplescom.MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="click here to detect whether the sd card is available or not"
        android:id="@+id/button"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="SD CARD STATUS"
        android:id="@+id/textView"
        android:layout_above="@+id/button"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="38dp"
        android:gravity="center"/>

</RelativeLayout>

Code for AndroidManifest.xml file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android_examples.detectsdcard_android_examplescom">

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Screenshots:

sd-card-android-1

sd-card-android-2

sd-card-android-3

Detect Check SD Card is present or not in android with Runtime permission

Click here to download Detect Check SD Card is present or not in android with Runtime permission project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *