[英]permission denied on creating new file in external storage
I want to create a new file in external storage if that file doesn't exist already. 如果该文件不存在,我想在外部存储中创建一个新文件。 I've already read similar questions in SO and have
WRITE_EXTERNAL_STORAGE
permission in my manifest. 我已经在SO中阅读了类似的问题,并且在我的清单中有
WRITE_EXTERNAL_STORAGE
权限。 Testing on GenyMotion emulator with android 5.1 and xperia t with android 4.3 the result is same and I get " open failed: EACCES (Permission denied) " on file.createNewFile()
. 使用Android 5.1和xperia t使用Android 4.3测试GenyMotion模拟器,结果相同,我在
file.createNewFile()
上获得“ open failed:EACCES(Permission denied) ”。 I checked in runtime and getExternalStorageState
functoin return value is " MOUNTED" . 我在运行时检查并且
getExternalStorageState
functoin的返回值是“MOUNTED ” 。
Note: If I create the file manually, my code works perfectly and reads the content meaning that accessing to external storage is OK. 注意:如果我手动创建文件,我的代码可以正常工作并读取内容,这意味着访问外部存储是可以的。 I although write to external storage another place in my code using
getExternalPublicStorage
for saving captured image and it works fine! 我虽然使用
getExternalPublicStorage
写入外部存储器在我的代码中的另一个地方用于保存捕获的图像,但它工作正常!
File f = Environment.getExternalStorageDirectory();
File config = new File(f, "poinila config" + ".txt");
if (!config.exists()) {
if (!config.createNewFile()) {
// toast that creating directory failed
} else {
writeDefaultIpPort();
}
}
Edit: path string is "/storage/sdcard0/poinila config.txt" 编辑:路径字符串是“/ storage / sdcard0 / poinila config.txt”
well I think this is the new security feature introduced in android. 我认为这是android中引入的新安全功能。 Runtime permissions.
运行时权限。 You have to do something like this
你必须做这样的事情
int hasReadExternalStoragePermission = ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE);
if(hasReadExternalStoragePermission != PackageManager.PERMISSION_GRANTED)
if (shouldShowRequestPermissionRationale(Manifest.permission.READ_EXTERNAL_STORAGE))
new AlertDialog.Builder(getContext())
.setMessage("You need to Allow access to Images to set a Company Logo")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
requestPermission();
}
}).show();
else
requestPermission();
else
callChooser();
Is your permission is right place in manifest file as 您的权限是否在清单文件中是正确的位置
<application>
...
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
</manifest>
I had the same issue. 我遇到过同样的问题。 I think it is a missing / in the path.
我认为这是一个缺失/在路径。 Try this:
尝试这个:
config = new File(Environment.getExternalStorageDirectory() + "/" + "poinila config.txt" );
Hope it helps 希望能帮助到你
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.