Mkdir没有工作。在日志中,我收到以下消息:
12-23 15:51:18.290 21326-21326 / com.example.joche.mywarranty E / File:fail readDirectory()errno = 2
12-23 15:51:21.050 21326-21326 / com.example.joche.mywarranty W / System.err:mkdir失败:EROFS(只读文件系统):/ testom.example.joche.mywarranty E / File:fail readDirectory()errno = 2
我尝试过排查并在AndroidManifest文件中拥有以下权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
这是我的代码:
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE }, 100);
}
File newFile = new File("/test");
if (!newFile.exists() || !newFile.isDirectory())
{
newFile.mkdir();
}
还要求运行时权限:
我怎么解决这个问题?我可能没有正确的权限吗?
对于Android 6+,您需要添加代码以要求用户确认所请求的权限。
谷歌的运行时权限。
File newFile = new File("/test");
那永远不会成功。尝试外部存储:
File newFile = newFile(Environment.getExternalStorageDirectory(), "test");