Android 为什么授予权限后仍然获得权限拒绝异常
最主要的是Android 为什么授予权限后仍然获得权限拒绝异常,android,file-permissions,Android,File Permissions,最主要的是 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 如果不返回true,它将请求权限 ActivityCompat.requestPermissions(activity, new String[] { Ma
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
如果不返回true,它将请求权限
ActivityCompat.requestPermissions(activity, new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, 5);
只有当它拥有权限时,才会尝试创建该文件
File directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
String sysDownloadPath = directory.getPath();
try {
if (!directory.exists() && !directory.mkdirs()) {
//shouldn't happen in here
/* Checks if external storage is available for read and write */
String state = Environment.getExternalStorageState();
String externalStorageAvailable = (Environment.MEDIA_MOUNTED.equals(state)) ?
"ext-ST available" :
"ext-ST NOT available"; }
} catch (Exception e) { //<=== did not caught exception here
}
String fileName = ‘SD_20170404.pdf’;
String newFilePath = (sysDownloadPath + "/" + fileName);
File newFile = new File(newFilePath);
try {
newFile.createNewFile();// <=== throws at this one
} catch (Exception e) {
fatal Exception: java.lang.Exception: open failed: EACCES (Permission denied), newFile.getPath():/storage/emulated/0/Download/SD_20170404.pdf
File directory=Environment.getExternalStoragePublicDirectory(Environment.directory\u下载);
字符串sysDownloadPath=directory.getPath();
试一试{
如果(!directory.exists()&&!directory.mkdirs()){
//不应该发生在这里
/*检查外部存储器是否可用于读写*/
String state=Environment.getExternalStorageState();
字符串externalStorageAvailable=(Environment.MEDIA_MOUNTED.equals(state))?
“ext ST可用”:
“ext ST不可用”;}
}catch(异常e){//NEVER HARDCODE path。/storage/emulated/0/
不是所有设备上的正确目录。请使用方法(getExternalFilesDir()
,getExternalCacheDir()
,以及环境
上的方法等)为当前设备生成有效路径。请显示完整代码。谢谢,Commonware!,该路径是从异常复制的,只是为了显示问题。它在应用程序中不是硬编码的。它正在执行文件目录=环境。getExternalStoragePublicDirectory(环境。目录\u下载);该下载目录是否存在?您应该添加代码来检查。如果(!newFile.getParentFile().exists())返回;
。也显示一个toast以通知用户。路径来自File directory=Environment.getExternalStoragePublicDirectory(Environment.DI目录(下载);并将新文件名添加到其中以创建新文件。不要硬编码路径。/storage/emulated/0/
不是所有设备上的正确目录。请使用方法(getExternalFilesDir()
,getExternalCacheDir()
,以及Environment
上的方法等)为当前设备生成有效路径。请显示完整代码。谢谢,Commonware!,该路径是从异常复制的,只是为了显示问题。它在应用程序中不是硬编码的。它正在执行文件目录=环境。getExternalStoragePublicDirectory(环境。目录\u下载);该下载目录是否存在?您应该添加代码来检查。如果(!newFile.getParentFile().exists())返回;
。也显示一个toast以通知用户。路径来自File directory=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);并将新文件名附加到其中以创建新文件。
File directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
String sysDownloadPath = directory.getPath();
try {
if (!directory.exists() && !directory.mkdirs()) {
//shouldn't happen in here
/* Checks if external storage is available for read and write */
String state = Environment.getExternalStorageState();
String externalStorageAvailable = (Environment.MEDIA_MOUNTED.equals(state)) ?
"ext-ST available" :
"ext-ST NOT available"; }
} catch (Exception e) { //<=== did not caught exception here
}
String fileName = ‘SD_20170404.pdf’;
String newFilePath = (sysDownloadPath + "/" + fileName);
File newFile = new File(newFilePath);
try {
newFile.createNewFile();// <=== throws at this one
} catch (Exception e) {
fatal Exception: java.lang.Exception: open failed: EACCES (Permission denied), newFile.getPath():/storage/emulated/0/Download/SD_20170404.pdf