Android13-访问PDF或其他类型文件的权限
创始人
2024-09-30 23:00:53
0

为应用添加文件读取权限。具体代码示例如下:

在AndroidManifest.xml文件中添加以下权限:


在Java代码中获取Read External Storage权限:

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
}

在回调方法中处理权限请求的结果:

@Override
public void onRequestPermissionsResult(int requestCode,
        String[] permissions, int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the
                // contacts-related task you need to do.

            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request.
    }
}

相关内容

热门资讯

分享透视"wepok... 分享透视"wepoker发牌的套路"开挂(透视)辅助安装(带你了解2026新版教程)分享透视"wep...
原来有透视"wepo... 原来有透视"wepoker透视挂教程"开挂(透视)辅助脚本(重大消息实用技巧)原来有透视"wepok...
正品透视"wepok... 正品透视"wepoker开挂辅助挂教程"开挂(透视)辅助挂(玩家必看科普规律教程)正品透视"wepo...
曝光透视"WePok... 曝光透视"WePoker辅助挂教程"开挂(透视)辅助挂(终于懂了解密教程)《曝光透视"WePoker...
原来有透视"wepo... 原来有透视"wepokerplus有没有挂"开挂(透视)辅助神器(详细说明解密教程)原来有透视"we...
正品透视"WePok... 正品透视"WePoker德州开挂神器透视"开挂(透视)辅助平台(一分钟了解科技教程)正品透视"WeP...
科技透视"wepok... 科技透视"wepoker真的有透视挂么"开挂(透视)辅助工具(免费测试版安装教程)科技透视"wepo...
实测透视"微扑克发牌... 实测透视"微扑克发牌真的很假"开挂(透视)辅助插件(实操分享透明教程)实测透视"微扑克发牌真的很假"...
传授透视"WePok... 传授透视"WePoker透视插件怎么安装"开挂(透视)辅助器(2026教程必胜教程)传授透视"WeP...
实测透视"wepok... 实测透视"wepoker有挂吗开挂"开挂(透视)辅助插件(一分钟了解专业教程)实测透视"wepoke...