猿问

Android 9 无法在 Android 外部公共路径

我想在 Android 外部公共路径 ( ) 中创建一个目录"storage/emulated/0/MyImages"。


该代码在 Android 9 上运行。授予所有在外部存储上写入的运行时权限。


试过这样的代码。


File dir = new File(Environment.getExternalStorageDirectory() + File.separator + "MyImages");

if (!dir.exists) {

  dir.mkdirs();

}

该dir变量具有路径 - "/storage/emulated/0/MyImages"。


dir.mkdirs()总是返回false。LogCat 有一个简短的日志:


W/System.err: java.io.IOException: No such file or directory

W/System.err: at java.io.UnixFileSystem.createFileExclusively0(Native Method)

W/System.err: at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:281)

W/System.err: at java.io.File.createNewFile(File.java:1008)


POPMUISE
浏览 144回答 1
1回答

哔哔one

首先,如果是,请检查权限然后调用您的函数来调用 showPhoneStatePermission在清单中添加权限<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />private void showPhoneStatePermission() {&nbsp; &nbsp; int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);&nbsp; &nbsp; if (permissionCheck != PackageManager.PERMISSION_GRANTED) {&nbsp; &nbsp; &nbsp; &nbsp; if (ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; showExplanation("Permission Needed", "Rationale", Manifest.permission.WRITE_EXTERNAL_STORAGE, REQUEST_PERMISSION_WRITE_EXTERNAL_STORAGE);&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; requestPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, REQUEST_PERMISSION_WRITE_EXTERNAL_STORAGE);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(MainActivity.this, "Permission (already) Granted!", Toast.LENGTH_SHORT).show();&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答