即使使用WRITE_EXTERNAL_STORAGE
授予访问文件的运行时权限,也需要重新启动应用程序我尝试在应用程序开头的SplashScreen
类自我
我用
Manifest.permission.WRITE_EXTERNAL_STORAGE
在应用程序本身的开头
这是我试过的代码
private void requestPermission() {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
ShowAlert();
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
PERMISSION_REQUEST_CODE);
}
}
public void ShowAlert() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("PERMISSION REQUIRED ...");
alertDialog.setMessage("PERMISSION REQUIRED TO READ FILES");
alertDialog.setPositiveButton("GRANT PERMISSION", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(SplashScreen.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
PERMISSION_REQUEST_CODE);
dialog.cancel();
}
});
alertDialog.setNegativeButton("NO ", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
然后在service
我尝试使用
try {
mediaPlayer.reset();
mediaPlayer.setDataSource(song_Path);
mediaPlayer.prepare();
mediaPlayer.start();
Paused = false;
} catch (IllegalArgumentException e) {
} catch (SecurityException e) {
} catch (IllegalStateException e) {
} catch (IOException e) {
}
如果如上所述授予实时权限,则不需要重新启动应用程序,但是当我在try catch
授予权限后尝试首次访问文件时,我得到IOException
声明未授予权限且文件无法访问,一旦我重新启动应用程序,就可以访问该文件
需要做什么,以便第一次不需要重新启动应用程序来访问该文件