简体   繁体   English

使用 Kotlin 写入 Android 上的 SD 卡

[英]With Kotlin write to SD-card on Android

I have read a lot of questions and answers, but are not really satisfied and successful My Problem: write with Kotlin to a sdcard to a specific directory我已经阅读了很多问题和答案,但并不真正满意和成功我的问题:用 Kotlin 写入 sdcard 到特定目录

Working is工作是

        var filenamex = "export.csv"
        var patt = getExternalFilesDirs(null)
        var path = patt[1]
//create fileOut object
        var fileOut = File(path, filenamex)
//create a new file
        fileOut.createNewFile()

with getExternalFIlesDirs() I get the external storage and the sdcard.使用 getExternalFIlesDirs() 我得到了外部存储和 SD 卡。 With path = patt[1] i get the adress of my sd-card.使用 path = patt[1] 我得到了我的 sd 卡的地址。

this is这是

"/storage/105E-XXXX/Android/data/com.example.myApp/files"

This works to write data in this directory.这可以在此目录中写入数据。 But I would like to write into an other directory, for example但我想写入另一个目录,例如

"/sdcard/myApp"

A lot of examples say, this should work, bit it does not.很多例子说,这应该有效,但它没有。 So I tried to take所以我试图采取

"/storage/105E-XXXX/myApp"

Why doesn't it work?为什么它不起作用? Ist the same beginning of storage /storage/105E-XXXX/, so it is MY sd-card.? storage/storage/105E-XXXX/的开头是否相同,所以是MY sd-card。? As I mentioned, it works on the sd-card, so it is not a problem of write-permission to the sdcard?正如我所提到的,它适用于 sd 卡,所以它不是 sdcard 的写权限问题吗?

Any idea?任何想法? (I also failed with FileOutputStream and other things) (我也失败了 FileOutputStream 和其他事情)

Try Out this..试试这个..

    var filenamex = "export.csv"

    var path = getExternalStorageDirectory() + "//your desired folder"

    var fileOut = File(path, filenamex)

    fileOut.createNewFile()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM