androidQ 访问SD卡权限问题

一、在AndroidManifest.xml中加入android:requestLegacyExternalStorage="true" ,如下:



    
    
    
    
    
        
            
                

                
            
        
    

二,动态申请权限

private void initPermission(Activity activity){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
                    ||ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED
            ) {
                // 检查权限状态
                if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                    //  用户彻底拒绝授予权限
                } else {
                    //  用户未彻底拒绝授予权限
                    ActivityCompat.requestPermissions(activity, PermissionUtil.PERMISSIONS, PermissionUtil.REQUEST);
                }
            }else{
                Toast.makeText(activity,"已经授权 : "+Manifest.permission.WRITE_EXTERNAL_STORAGE,Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    public void  onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 1) {
            for (int i=0;i

public class PermissionUtil {

    public static final int REQUEST = 1;
    public static String[] PERMISSIONS = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_SMS};
}

 

你可能感兴趣的:(android)