简体   繁体   English

无法显示android权限请求对话框

[英]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.粘贴在 android 开发人员站点示例中,说明如何启用权限,并在 android 开发工作室中从头开始提供错误。

"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)" “错误的第一个参数类型。发现:'android.content.Context',需要:'android.app.Activity'检查信息:ActivityCompat 中的 shouldShowRequestPermissionRationale (android.app.Activity,String) 不能应用于 (android.content.Context) ,细绳)”

using (activity)this works to remove the error, but doesn't give much trust in the android docs at this point.使用 (activity)this 可以消除错误,但此时对 android 文档不太信任。 Still can't get the permission dialog to come up.仍然无法获得权限对话框。 Try several other examples and Stackoverflow "solutions" with no joy.不高兴地尝试其他几个示例和 Stackoverflow“解决方案”。

finish basic app from scratch, stick in some printlns to show progress, no dialog in the emulator or on my phone.从头开始完成基本的应用程序,坚持一些 printlns 以显示进度,在模拟器或我的手机上没有对话框。 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. Logcat 显示进程中的一切似乎都在工作,它发现不允许的权限,分支到请求它的部分,然后测试以查看响应是否允许它,除了它实际打开对话框的部分。 I've had no luck finding anything relevant on the egl errors.我没有找到与 egl 错误相关的任何内容。 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.我希望弹出一个对话框,允许我授予在下载文件夹中打开文件的必要权限。

Your manifest has:你的清单有:

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

Your Java code does not refer to READ_EXTERNAL_STORAGE .您的 Java 代码未引用READ_EXTERNAL_STORAGE It refers to WRITE_EXTERNAL_STORAGE .它指的是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如果 #1 的答案是“是的,我需要写”,请将您的清单切换为引用WRITE_EXTERNAL_STORAGE

  3. If the answer to #1 is "no, reading is fundamental", switch your Java code to refer to READ_EXTERNAL_STORAGE如果 #1 的答案是“不,阅读是基础”,请切换您的 Java 代码以引用READ_EXTERNAL_STORAGE

FWIW, this sample app from this book demonstrates the use of WRITE_EXTERNAL_STORAGE . FWIW,本书中的这个示例应用程序演示了WRITE_EXTERNAL_STORAGE的使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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