报错信息:
其实这个问题是由于Android6.0更新了权限机制,在6.0之前,写入sd卡权限只需在清单文件中添加
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
即可,而在6.0及以上版本,对一些公共目录的访问比如:/storage/emulated/0/Download
则需要在activity中用代码来请求一些敏感的权限方可实现,其中就包括对sd卡的操作权限。对这个问题有以下几种解决办法:
- 打开虚拟机的Setting–>Apps–>找到你的应用–>点击Permissions–>将需要的权限手动打开
- 将targetSdkVersion设置为小于23,然后重新编译
- 手动在activity添加请求权限的代码,具体代码可参考如下简书链接:
Android 6.0 运行时权限处理
另:Android10+ 以后还需要在AndroidManifest.xml application 标签中添加 android:requestLegacyExternalStorage="true"
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:requestLegacyExternalStorage="true"
tools:targetApi="q"
android:supportsRtl="true"
android:theme="@style/AppTheme" >