我一直试图在Android Studio中使用Kotlin创建一个简单的路径,但遗憾的是我无法这样做。 现在,我的代码看起来像这样:
lateinit var context : Context
private const val PERMISSION_REQUEST = 10
fun makeDir(){
// I/O logic
val filename: String = "ziemniakList.txt"
val myDir: String = Environment.getDataDirectory().toString() + "/Ziemniak/"
// create a File object for the parent directory
val sciezka = File(myDir)
// have the object build the directory structure, if needed.
sciezka.mkdirs()
// create a File object for the output file
//val outputFile = File(sciezka, filename)
//println("makeDir!")
Toast.makeText(context, myDir, Toast.LENGTH_LONG).show()
}
class NowaDostawa() : AppCompatActivity() {
private var permissions = arrayOf<String>(android.Manifest.permission.WRITE_EXTERNAL_STORAGE,android.Manifest.permission.READ_EXTERNAL_STORAGE)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_nowa_dostawa)
var MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 1
context=this
val menuLista = arrayOf("Online", "Gotówka", "Karta")
val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, menuLista)
spinner.adapter = adapter
buttonNoweZamowienie.setOnClickListener{
if(inputAdres.text.toString() == "" || inputKwota.text.toString() == ""){
Toast.makeText(this, "All the fields needs to be filled!", Toast.LENGTH_LONG).show()
}
else{
var allSuccess = 1
for(i in permissions.indices){
if(checkCallingOrSelfPermission(permissions[i]) == PackageManager.PERMISSION_DENIED){
allSuccess = 0
}
}
if(allSuccess == 1){
Toast.makeText(context, "Permissions granted!", Toast.LENGTH_LONG).show()
makeDir()
finish()
}
else{
requestPermissions(permissions, PERMISSION_REQUEST)
}
}
}
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if(requestCode == PERMISSION_REQUEST){
var allSuccess = true
for(i in permissions.indices){
if(grantResults[i] == PackageManager.PERMISSION_DENIED){
allSuccess = false
var requestAgain = shouldShowRequestPermissionRationale(permissions[i])
if(requestAgain){
Toast.makeText(context, "Permissions hasn't been granted!", Toast.LENGTH_LONG).show()
}
else{
Toast.makeText(context, "Set permissions by yourself!", Toast.LENGTH_LONG).show()
}
}
}
if(allSuccess){
Toast.makeText(context, "Permissions has been granted!", Toast.LENGTH_LONG).show()
makeDir()
finish()
}
}
}
所以基本上,当我点击按钮时,按照Zamowienie&#39;按钮,文件夹应该在/ data / Ziemniak /下创建,但它没有。正如您所看到的,我授予权限并且它运行良好,但我仍然觉得很难创建一些基本目录。可能是什么问题?如果有一些我没有指定的事情,请随时询问
提前谢谢
答案 0 :(得分:0)
Read is implicitly permitted when write is granted.
您是否已在应用程序的清单中声明了该权限?
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
答案 1 :(得分:0)
不要忘记在AndroidManifest.xml中添加它
android:requestLegacyExternalStorage="true"