Android m运行时允许从外部存储读取

时间:2016-03-29 09:49:39

标签: android-6.0-marshmallow runtime-permissions

我有一个对话框,可以加载带有5个部分的tabLayout,并使用带有View Pager适配器的ViewPager加载其内容。现在,在其中一个部分中,我必须从库中加载图像。我已经编写了运行时权限,但我无法使用权​​限的回调,因为我已经在没有活动的类中实现了此对话框。我不知道如何在无活动类中实现权限的回调方法。

public void makePermissionRequest(){ if(ContextCompat.checkSelfPermission(showEditorActivity, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(showEditorActivity, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, permission_request_code); }else{ fetchImagesFromDevice(); } }

和回调..

 @Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode) {
        case permission_request_code: {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Log.i("permission..link", "allowed");
                fetchImagesFromDevice();

            }else{
                Log.i("permission..link", "denied");
                if (ContextCompat.checkSelfPermission(showEditorActivity, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                    if (ActivityCompat.shouldShowRequestPermissionRationale(showEditorActivity, Manifest.permission.READ_EXTERNAL_STORAGE)) {
                        showMessageOKCancel("You need to allow access to your Internal Memory", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                //log to find out answer..
                                if (which == -1) {
                                    makePermissionRequest();
                                }
                            }
                        });
                    }

                }
            }

        }

    }
}

0 个答案:

没有答案