App Internal or External Storage Permission Android 6.0 (Marshmallow) and up in Android.


When you install an app from Google Play on a device running Android 6.0 and up or on a device, you control which capabilities or information that app can access - known as permissions. You can control which permissions an app can access after the app installs on your device.

All Android devices have two file storage areas: "internal" and "external" storage.

Create a new Andorid Project

Open AndroidManifest.xml file and add following permission.

AndroidManifest.xml


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

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_INTERNAL_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"
            android:screenOrientation="portrait">

        </activity>
        <activity
            android:name=".PermissionActivity"
            android:theme="@style/Theme.AppCompat.NoActionBar"
            android:screenOrientation="portrait">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        </activity>
    </application>

</manifest>
	
	

Create a new Activity that take a permission. Permission ask before a main class called.

PermissionActivity.java

									   
package com.nk.permissionrequire;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import java.util.ArrayList;
import java.util.List;

public class PermissionActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_permission);
        context = this;
        checkPermissions();
    }

    Context context;
    Boolean per = false;
    String[] permissions = new String[]{
            Manifest.permission.INTERNET,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
    };

    private boolean checkPermissions() {
        int result;
        List listPermissionsNeeded = new ArrayList<>();
        for (String p : permissions) {
            result = ContextCompat.checkSelfPermission(this, p);
            if (result != PackageManager.PERMISSION_GRANTED) {
                listPermissionsNeeded.add(p);
               Log.w("-----------", "granteddddd ddddddddddd ");
            } else {
                Log.w("-----------", "granteddddd elseeeeeeeeeeeeeeee");

            }
        }
   if (!listPermissionsNeeded.isEmpty()) {
      ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray
	  (new String[listPermissionsNeeded.size()]), 100);
            Log.w("-----------", "granteddddd  iffff");
            return false;
        } else {
            Log.w("-----------", "granteddddd 666666666(");
            startActivity(new Intent(context, MainActivity.class));
            finish();
        }
        return true;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, 
	@NonNull String[] permissions, @NonNull int[] grantResults) {
	
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 100) {
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // do something
                Log.w("-----------", "granteddddd ");
                startActivity(new Intent(context, MainActivity.class));
                finish();
            }
            return;
        }
    }
}

No need to anything implement in this xml file.

activity_permission.xml

									   						   
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

</LinearLayout>

It is main xml file you can perform any layout work here.

activity_main.xml

						   
<?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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerInParent="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:padding="20dp"
            android:text="Permission Given"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textStyle="bold" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="10dp"
            android:text="READ_EXTERNAL_STORAGE AND WRITE_EXTERNAL_STORAGE"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/colorPrimary" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="10dp"
            android:text="READ_INTERNAL_STORAGE AND WRITE_INTERNAL_STORAGE"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/colorPrimary" />

    </LinearLayout>

</RelativeLayout>
   

It is main JAVA class file you can perform any task here.

MainActivity.java

									   						   
package com.nk.permissionrequire;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

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

Android Develepment

  • Android

  • Android Studio

  • Android development