亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法使用 mkdir 在 Android Q 中創建文件夾

無法使用 mkdir 在 Android Q 中創建文件夾

嗶嗶one 2023-03-09 17:18:04
我的應用程序在所有 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();        }
查看完整描述

2 回答

?
鳳凰求蠱

TA貢獻1825條經驗 獲得超4個贊

<manifest ... >

 <!-- This attribute is "false" by default on apps targeting Android Q. ->

  <application android:requestLegacyExternalStorage="true" ... >


 </application>

</manifest>


查看完整回答
反對 回復 2023-03-09
?
手掌心

TA貢獻1942條經驗 獲得超3個贊

在 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) {

    val intent = Intent(Intent.ACTION_CREATE_DOCUMENT).apply {

        // Filter to only show results that can be "opened", such as

        // a file (as opposed to a list of contacts or timezones).

        addCategory(Intent.CATEGORY_OPENABLE)


        // Create a file with the requested MIME type.

        type = mimeType

        putExtra(Intent.EXTRA_TITLE, fileName)

    }


    startActivityForResult(intent, WRITE_REQUEST_CODE)

}

示例 src https://developer.android.com/guide/topics/providers/document-provider


查看完整回答
反對 回復 2023-03-09
  • 2 回答
  • 0 關注
  • 231 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號