java.io.FileNotFoundException: Permission denied 错误解决方法

在android6.0以后,需要runtime权限:requestPermissions方法。

具体如下:

  private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };
 //获取动态权限
        int hasWriteContactsPermission = ContextCompat.checkSelfPermission(MainActivity.this,
                Manifest.permission.READ_EXTERNAL_STORAGE);//android.manifest.xml
        if (hasWriteContactsPermission != PackageManager.PERMISSION_GRANTED) {
            if (!ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
                    Manifest.permission.READ_EXTERNAL_STORAGE)) {
                Log.d("yx","get permission");
                ActivityCompat.requestPermissions(MainActivity.this,
                        PERMISSIONS_STORAGE,
                        REQUEST_EXTERNAL_STORAGE);


            }
            Log.d("yx","get permission2");
            ActivityCompat.requestPermissions(MainActivity.this,
                    PERMISSIONS_STORAGE,
                    REQUEST_EXTERNAL_STORAGE);

        }

        Log.d("yx","wait for PERMISSION_GRANTED");
        while (( ContextCompat.checkSelfPermission(MainActivity.this,
                Manifest.permission.READ_EXTERNAL_STORAGE))!= PackageManager.PERMISSION_GRANTED) {

        }
       Log.d("yx","wait for PERMISSION_GRANTED finish");

还需要加入android-support-v4.jar库。就可以或许android6.0以上的SD卡权限了。

你可能感兴趣的:(Android-代码)