无法使用 mkdir 在 Android Q 中创建文件夹

我的应用程序在所有 android 版本中创建文件夹没有问题,除了android Q,我不知道为什么我的代码在 android q 中不起作用


这是我的代码:


File dir = new File("/sdcard/test");

       try{

              if(dir.mkdir()) {

                      Log.d("DOWNLOAD","Directory created");

              } else {

                     Log.d("DOWNLOAD","Directory is not created");

              }

            }catch(Exception e){

                  e.printStackTrace();

        }


哔哔one
浏览 198回答 2
2回答

凤凰求蛊

<manifest ... >&nbsp;<!-- This attribute is "false" by default on apps targeting Android Q. ->&nbsp; <application android:requestLegacyExternalStorage="true" ... >&nbsp;</application></manifest>

手掌心

在 Q 中,如果您想要访问不是普通音乐或媒体文件的文件,即在您的情况下,sdcard/test您需要执行以下操作:为了访问另一个应用程序创建的任何其他文件,包括“下载”目录中的文件,您的应用程序必须使用存储访问框架,它允许用户选择特定文件。引用来源: http: //developer.android.com/preview/privacy/scoped-storage// Here are some examples of how you might call this method.// The first parameter is the MIME type, and the second parameter is the name// of the file you are creating://// createFile("text/plain", "foobar.txt");// createFile("image/png", "mypicture.png");// Unique request code.private const val WRITE_REQUEST_CODE: Int = 43...private fun createFile(mimeType: String, fileName: String) {&nbsp; &nbsp; val intent = Intent(Intent.ACTION_CREATE_DOCUMENT).apply {&nbsp; &nbsp; &nbsp; &nbsp; // Filter to only show results that can be "opened", such as&nbsp; &nbsp; &nbsp; &nbsp; // a file (as opposed to a list of contacts or timezones).&nbsp; &nbsp; &nbsp; &nbsp; addCategory(Intent.CATEGORY_OPENABLE)&nbsp; &nbsp; &nbsp; &nbsp; // Create a file with the requested MIME type.&nbsp; &nbsp; &nbsp; &nbsp; type = mimeType&nbsp; &nbsp; &nbsp; &nbsp; putExtra(Intent.EXTRA_TITLE, fileName)&nbsp; &nbsp; }&nbsp; &nbsp; startActivityForResult(intent, WRITE_REQUEST_CODE)}示例 src https://developer.android.com/guide/topics/providers/document-provider
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java