1、问题描述:在Android 10上用fopen打开一个pcm文件,该文件是有读写权限,在AndroidManifest.xml也申请了媒体文件读写权限,但一直提示打开失败,errno的值为13,该值表示的就是权限问题。
pcmFile = fopen(pcmurl, "r");
if(pcmFile == NULL)
{
LOGE("open file error, errno is:%d",errno);
return;
}
2、原AndroidManifest.xml权限申请
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" tools:ignore="ProtectedPermissions" />
3、解决办法,手动申请权限和授予。 首先,在AndroidManifest.xml的application后面添加一行代码,不添加不成功,代码为:
<application
android:requestLegacyExternalStorage="true"
</application>
然后,是编写申请读写权限代码,然后在onCreate调用verifyStoragePermission(this)和APP启动时授权就能解决了。
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
"android.permission.READ_EXTERNAL_STORAGE",
"android.permission.WRITE_EXTERNAL_STORAGE"
};
public void verifyStoragePermission(Activity activity){
try{
int permission = ActivityCompat.checkSelfPermission(activity,"android.permission.WRITE_EXTERNAL_STORAGE");
if(permission!= PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(activity,PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
}
}catch (Exception e){
e.printStackTrace();
}
}
|