写入外部存储设备不能正常工作,直到我重新启动在Android M上的应用 [英] Writing external storage doesn't work until I restart the application on Android M

查看:907
本文介绍了写入外部存储设备不能正常工作,直到我重新启动在Android M上的应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有新的许可制度的问题。我修改了code问权限WRITE_EXTERNAL_STORAG​​E。但是,直到我重新启动应用程序(当然我允许它在对话框中)我的应用程序不能写入(或读)的SD卡。之后,它的正常工作。

I am having an issue with the new permission system. I modified my code to ask permission for WRITE_EXTERNAL_STORAGE. But my app cannot write to (or read from) the sd card until I restart application (of course I allowed it in the dialog). After that it's working fine.

这不是一个新的应用程序,我已经在清单中声明WRITE_EXTERNAL_STORAG​​E,并在早期的系统它的正常工作。

It's not a new app, I already declared WRITE_EXTERNAL_STORAGE in the manifest, and on earlier systems it's working fine.

我使用checkSelfPermission和requestPermissions方法,当我做一个读取或写入请求到SD卡我已经得到了我没有权限例外。

I'm using checkSelfPermission and requestPermissions methods and when I make a read or write request to the sd card I've got an exception that I don't have permission.

任何人有任何想法,我错过了什么?还是我碰到一个错误?

Anyone has any idea what did I miss? Or did I run into a bug?

SDK版本是最新的(更新在几个小时前)和我测试​​它在模拟器上。

SDK version is the latest (updated a few hours ago), and I'm testing it on the emulator.

推荐答案

您需要回调当用户允许 WRITE_EXTERNAL_STORAG​​E 的RuntimePermission对话框允许

首先,你需要执行 OnRequestPermissionsResultCallback 监听器 ActivityCompat 类在你的活动并重写无效onRequestPermissionsResult(INT申请code,@NonNull的String []的权限,@NonNull INT [] grantResults) 方法来检查用户是否被允许或拒绝许可

First you need to implement OnRequestPermissionsResultCallback Listener of ActivityCompat class in your Activity and override void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) Method to check whether User is allowing or denying the permission

您可以这样做:

int REQUEST_STORAGE = 1;

private void checkExternalStoragePermissions() {
    if (hasStoragePermissionGranted()) {
        //You can do what whatever you want to do as permission is granted
    } else {
        requestExternalStoragePermission();
    }
}

public boolean hasStoragePermissionGranted(){
        return  ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
}

public void requestExternalStoragePermission() {
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
        ActivityCompat.requestPermissions(MainActivity.this, {Manifest.permission.WRITE_EXTERNAL_STORAGE},
                REQUEST_STORAGE);
    }
}

下面是 onRequestPermissionsResult()方法将被调用时,用户允许或拒绝运行权限对话框权限。

Here is onRequestPermissionsResult() method will be called when user allow or deny Permission from Runtime permission dialog.

您还可以处理情况时,用户已签从不显示运行权限对话框。

You can also handle situation when User has checked never show Runtime Permission dialog.

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {

        if (requestCode == REQUEST_STORAGE) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
               //User allow from permission dialog
               //You can do what whatever you want to do as permission is granted
            } else if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivtity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
               //User has deny from permission dialog
               Snackbar.make(mainLayout, getResources().getString("Please enable storage permission"),Snackbar.LENGTH_INDEFINITE)
                      .setAction("OK", new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                ActivityCompat.requestPermissions(MainActivity.this, {Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_STORAGE);
                            }
                       })
                       .show();
            } else {
                // User has deny permission and checked never show permission dialog so you can redirect to Application settings page
                Snackbar.make(mainLayout, getResources().getString("Please enable permission from settings"),Snackbar.LENGTH_INDEFINITE)
                     .setAction("OK", new View.OnClickListener() {
                           @Override
                           public void onClick(View view) {
                                Intent intent = new Intent();                    intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                                Uri uri = Uri.fromParts("package", MainActivity.this.getPackageName(), null);
                                intent.setData(uri);
                                startActivity(intent);
                            }
                       })
                       .show();
            }
        }
}

在这里,我已经使用小吃吧来显示相关信息给用户有关权限和 mainLayout 是ID 活动的主要布局

Here i have used Snackbar to show relevant message to user about Permission and mainLayout is id of Activity's Main Layout.

希望它可以帮助你。

这篇关于写入外部存储设备不能正常工作,直到我重新启动在Android M上的应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆