Reputation: 57
I am attempting to create a Directory for my apps data which will be .txt files. At this point, I don't care if it is on the Internal and or External Storage. I have been searching all over for a solution and I can't find anything that works. MkDir() or MKDirs() has not worked for me.
The code I have posted is just the first of many attempts to resolve this problem. I keep trying to change the directory path. It will Detect that existing paths are there but If I set it to the path I desire to create it will not work. I do have permission to read and write external storage in my Manifest I tried to find code to request permission at runtime with no success. So I manually allowed storage through the device's settings for the app and still nothing.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
val f = File("/TestFolder")
if (f.exists() && f.isDirectory()) {
println("Exists")
} else {
f.mkdir()
println("Does Not Exist.")
}
Upvotes: 3
Views: 7528
Reputation: 362
Hope this works:
var filename = "blesson.txt"
try {
// create a File object for the parent directory
val wallpaperDirectory = File("/sdcard/Wallpaper/")
// have the object build the directory structure, if needed.
wallpaperDirectory.mkdirs()
// create a File object for the output file
val outputFile = File(wallpaperDirectory, filename)
// now attach the OutputStream to the file object, instead of a String representation
val fos = FileOutputStream(outputFile)
} catch (e: FileNotFoundException) {
e.printStackTrace()
}
Upvotes: 2
Reputation: 170919
"/TestFolder"
is an absolute path which is not part of external storage. It should be something like
val file = File(context.getExternalFilesDir(null), "TestFolder")
if (file.isDirectory()) { ... } else {
file.mkdirs()
...
}
(isDirectory
implies exists
so there's no need to check both).
Note also there is a distinction between private and public external storage (see https://developer.android.com/training/data-storage/files/external):
Public files: Files that should be freely available to other apps and to the user. When the user uninstalls your app, these files should remain available to the user. For example, photos captured by your app should be saved as public files.
Private files: Files stored in an app-specific directory—accessed using Context.getExternalFilesDir(). These files are cleaned up when the user uninstalls your app. Although these files are technically accessible by the user and other apps because they're on the external storage, they don't provide value to the user outside of your app. Use this directory for files that you don't want to share with other apps.
The above assumes you want a private directory.
Upvotes: 3
Reputation: 1422
before you create file you should to create folder and thent create file in that please try as below
val extStorageDirectory = Environment.getExternalStorageDirectory().toString()
val dir = File(extStorageDirectory)
if (!dir.exists())
dir.mkdirs()
val file: File
file = File(extStorageDirectory, "/TestFolder")
if (file.exists()) {
file.delete()
file.createNewFile()
} else {
file.createNewFile()
}
Upvotes: 4