user2192998
user2192998

Reputation: 13

Can't get the android permission request dialog to appear

Trying to read a file from the downloads directory for an application I'm working on, and I cannot for the life of me get it to open the dialog for the user to give permission to access files.

Trying to add the ability to import data from a file the user can download from another site. Attempting to open the file from the download folder is nothing but failure. Finally determine that it's definitely the permissions to access the file that aren't being enabled, and I can't get the dialog to allow permissions to ever come up. Give up and create a new app from scratch

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

paste in the android developer site example of how to enable permissions and that gives errors from scratch in the android development studio.

"Wrong 1st argument type. Found: 'android.content.Context', required: 'android.app.Activity' Inspection info: shouldShowRequestPermissionRationale (android.app.Activity,String) in ActivityCompat cannot be applied to (android.content.Context,String)"

using (activity)this works to remove the error, but doesn't give much trust in the android docs at this point. Still can't get the permission dialog to come up. Try several other examples and Stackoverflow "solutions" with no joy.

finish basic app from scratch, stick in some printlns to show progress, no dialog in the emulator or on my phone. Logcat shows that the everything seems to be working in the process, it's finding the permission no allowed, branching to the part to ask for it, then testing to see if the response allowed it, except for the part where it actually opens a dialog. I've had no luck finding anything relevant on the egl errors. Example logcat:

07-20 08:30:19.999 23735-23735/anticlimacticteleservices.myapplication I/System.out: need to ask for permission
07-20 08:30:20.014 23735-23775/anticlimacticteleservices.myapplication D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
07-20 08:30:20.049 23735-23775/anticlimacticteleservices.myapplication I/OpenGLRenderer: Initialized EGL, version 1.4
07-20 08:30:20.050 23735-23775/anticlimacticteleservices.myapplication W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
07-20 08:30:20.096 23735-23775/anticlimacticteleservices.myapplication D/EGL_emulation: eglCreateContext: 0xaaa757e0: maj 2 min 0 rcv 2
07-20 08:30:20.099 23735-23775/anticlimacticteleservices.myapplication D/EGL_emulation: eglMakeCurrent: 0xaaa757e0: ver 2 0 (tinfo 0xac396210)
07-20 08:30:20.133 23735-23775/anticlimacticteleservices.myapplication D/EGL_emulation: eglMakeCurrent: 0xaaa757e0: ver 2 0 (tinfo 0xac396210)
07-20 08:30:20.181 23735-23735/anticlimacticteleservices.myapplication I/System.out: You denied write external storage permission.
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;


public class MainActivity extends AppCompatActivity {
    private static final int PERMISSION_REQUEST_CODE = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Context thisActivity = this;
        try{
            int writeExternalStoragePermission = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
            if(writeExternalStoragePermission!= PackageManager.PERMISSION_GRANTED){
               System.out.println("need to ask for permission");
               ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE);
            }else {
                System.out.println("it actually has permission now");
            }
        }catch (Exception ex)        {
            System.out.println("failed to get permissions with "+ex.getMessage());
        }
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions,  int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(requestCode == PERMISSION_REQUEST_CODE)        {
            int grantResultsLength = grantResults.length;
            if(grantResultsLength > 0 && grantResults[0]==PackageManager.PERMISSION_GRANTED) {
                System.out.println("You did it finally");
            }else {
                System.out.println("You denied write external storage permission.");
            }
        }
    }

}

I expect a dialog to pop up allowing me to grant the neccesary permission to open a file in the download folder.

Upvotes: 1

Views: 206

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007658

Your manifest has:

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

Your Java code does not refer to READ_EXTERNAL_STORAGE. It refers to WRITE_EXTERNAL_STORAGE. Those need to match.

To fix this:

  1. Decide whether you need to write or not

  2. If the answer to #1 is "yes, I need to write", switch your manifest to refer to WRITE_EXTERNAL_STORAGE

  3. If the answer to #1 is "no, reading is fundamental", switch your Java code to refer to READ_EXTERNAL_STORAGE

FWIW, this sample app from this book demonstrates the use of WRITE_EXTERNAL_STORAGE.

Upvotes: 1

Related Questions