简体   繁体   中英

Not asking external storage permission on Android studio

my name is Tom, i am trying to open a file from the external storage. for some reason it is not asking the permission at all?!?! I will thank for any answer! Tom. Here is my code:

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
            Toast.makeText(MainActivity.this,"should snd request ",Toast.LENGTH_SHORT).show();

            requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, PERMISSION_REQUET_CODE);
        }

public void OnRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResult){
        if(requestCode == PERMISSION_REQUET_CODE){
            if(grantResult[0] == PackageManager.PERMISSION_GRANTED){
                Toast.makeText(MainActivity.this,"permission granted!",Toast.LENGTH_SHORT).show();
            }
            else{
                Toast.makeText(MainActivity.this,"*** permission  not granted! ***",Toast.LENGTH_LONG).show();
                finish();
            }
        }
    }

Make sure you also have added permissions to the Manifest file

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM