在API23+以上,不止要在AndroidManifest.xml里面添加权限
1 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 2 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
还要在JAVA代码中请求权限:
1 // Storage Permissions 2 private static final int REQUEST_EXTERNAL_STORAGE = 1; 3 private static String[] PERMISSIONS_STORAGE = { 4 Manifest.permission.READ_EXTERNAL_STORAGE, 5 Manifest.permission.WRITE_EXTERNAL_STORAGE }; 6 7 /** 8 * Checks if the app has permission to write to device storage 9 * 10 * If the app does not has permission then the user will be prompted to 11 * grant permissions 12 * 13 * @param activity 14 */ 15 public static void verifyStoragePermissions(Activity activity) { 16 // Check if we have write permission 17 int permission = ActivityCompat.checkSelfPermission(activity, 18 Manifest.permission.WRITE_EXTERNAL_STORAGE); 19 20 if (permission != PackageManager.PERMISSION_GRANTED) { 21 // We don't have permission so prompt the user 22 ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE, 23 REQUEST_EXTERNAL_STORAGE); 24 } 25 }
在报错的方法前面调用即可
官方文档:http://developer.android.com/training/permissions/requesting.html
在Android API级别23及以上,除了在AndroidManifest.xml中声明权限,还需要在运行时通过JAVA代码请求权限。错误'Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)'表明缺少必要的运行时权限,解决方法是在相关方法前调用权限请求。

被折叠的 条评论
为什么被折叠?



