简体   繁体   中英

Able to read file in android even when user permission denied

I'm trying to learn about programming in Android. Not sure what I am doing wrong but I request permission to read from SDCARD and when testing if I deny the request I can still read the file?!

In the manifest I have:

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

And in the code, I check for permissions but, as a test, I immediately call the code to open and read the file regardless of what the permission result is...and somehow I can still read the file.

Code to read the file:

    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.setType("text/*");
    startActivityForResult(intent, READ_REQ);

I know I should check the result of the permission request before attempting to open the file but shouldn't the Android system block me from being able to read the file if permission is not granted?

(I'm using a Poco F1, MIUI 10 in case that has some abnormalities!!)

Edit: added code that obtains the file (reduced for brevity):

public void onActivityResult(int requestCode, int resultCode,
                             Intent resultData) {

    if (resultCode == Activity.RESULT_OK) {

        Uri uri = null;
        if (resultData != null) {
            uri = resultData.getData();

        }

        if(requestCode == READ_REQ){
            numbers = readFile(uri);
        }
    }
}

And

private ArrayList<String> readFile(Uri uri)
{
    ArrayList<String> records = new ArrayList<String>();
    ArrayList<String> errors = new ArrayList<String>();
    InputStream inputStream;
    try
    {
        inputStream = getContentResolver().openInputStream(uri);
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                inputStream));
        String line;

        while ((line = reader.readLine()) != null)
        {
           // Reads each line in to the array.
        }

        reader.close();
        return records;
    }
    catch (Exception e)
    {
        Log.i(LOG_TAG, "Error: " + e);
        //System.err.format("Exception occurred trying to read '%s'.", file);
        e.printStackTrace();
        return null;
    }
}

not possible to view without permission so you need to ask every time user for allow this persmission when user open

// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
    Manifest.permission.READ_EXTERNAL_STORAGE)
    != PackageManager.PERMISSION_GRANTED) {

// Permission is not granted
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
        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.
} else {
    // No explanation needed; request the permission
    ActivityCompat.requestPermissions(thisActivity,
            new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
            MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);

    // MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE is an
    // app-defined int constant. The callback method gets the
    // result of the request.
}
} else {
 // Permission has already been granted
}

If you deployed app in debug mode from Android Studio permissions will be granted by default. Open your application properties on device where your app is running and check granted permissions. When you disable in settings, your app won't ask permissions, it won't be granted and you will unable to write to external storage. Also, make sure your device is Android 6.0 or newer. On Android older than 6.0 all permissions are always granted.

In summary - try to deny access from system settings.

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