Unable to create folder in internal directory - android

I am trying to create folder in Android internal storage. For that I am using below code
val path = File(getExternalFilesDir(null),"MyFolder")
But it is creating folder in Android directory inside app package name under files directory like this: Android/data/com.app.myapp/files/MyFolder. I don't want to create folder like this rather I want to create folder in internal storage like WhatsApp creates.
How can I do this?

So easy, for API level 29 or later:
binding.createFolderButton.setOnClickListener {
val values = ContentValues()
values.put(MediaStore.MediaColumns.RELATIVE_PATH, "${Environment.DIRECTORY_DOCUMENTS}/myFolder/") //folder name
contentResolver.insert(MediaStore.Files.getContentUri("external"), values)
Toast.makeText(this, "\"myFolder\" created", Toast.LENGTH_SHORT).show()
}
Demo: https://youtu.be/a6Q7IlA_uOs
Helpful video: https://youtu.be/UnJ3amzJM94

Related

I want to store a .7z file into specific folder in phone internal storage using kotlin

I have found a code on stackoverflow,
val data: String = "hello 123"
val path = this.getExternalFilesDir(null)
val folder = File(path, "folder1")
folder.mkdirs()
println(folder.exists()) // u'll get true
val file = File(folder, "message.txt")
file.appendText(data)
by using this I was able to achieve this :-
// internal storage --> Android /data/com.example.testing123/files/folder 1/message.txt --> hello 123
but what I want is:-
// internal storage --> Android/data/inside a already available folder (check if folder is there or not, if not then create it) com.heaven.supermaxgame/store a .7z file
So please help me, I am new to kotlin, how can I store a 7zip file into a specific folder inside android --> data -->specific folder

How to copy a .txt file in data folder into the Download folder in Android using Kotlin?

I want to copy a .txt file that's in my app's package folder to the Download folder when a button is pressed. This is the code for the button:
binding_second.btnDone.setOnClickListener(object : View.OnClickListener {
override fun onClick(v: View?) {
var this_dir = getExternalFilesDir(this_filename)
var target_dir = File("sdcard/Download/"+ this_filename)
this_dir?.copyTo(target_dir)
println("Copy succeeded")
}
})
When this code is run, instead of copying the .txt file and its content from the source location and making a copy in the Download folder, it creates a folder with the name of the .txt file within the Download folder. I would appreciate any help at all!
You're confused about how getExternalFilesDir works. The parameter to it is NOT a filename within the directory. It's the type of external file directory, such as Environment.DIRECTORY_PICTURES for pictures. Since you passed an invalid value, you're getting the default directory. Then you're copying that directory.
What you want instead is var this_dir = File(getExternalFilesDir(null), this_filename); That will get a file with the given name relative to the getExternalFilesDir directory.

Creating a folder in Android External Storage

I would like to create a directory in /storage/emulated/0/ and a save a file there.Since the "getExternalStorageDir()" & "getExternalPublicStorageDir()" are Depreciated I don't know how to implement it.
I have gone through most of the questions and answers but they are all either outdated/open.
All I want is a way to access the "storage/emulated/0/" path.
val extStorageDirectory = requireContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)
val folder = File(extStorageDirectory, "Work Logs")
folder.mkdir()
The above code creates a folder in "/storage/emulated/0/Android/data/com.xxx.xxx/files"

Folders not created using mkdirs() in android

I am creating folders using mkdirs() method. Before that, i am also checking if folder is already present or not. if not then only create. It works fine almost all devices but somehow its not creating in some devices.
I also checked runtime permission and storage access framework before creating folders. its all good still it does not create folders. Below is the sample path of creating folders:
Path: /storage/emulated/0/MyAppFolder/TestFolder
Here, /storage/emulated/0/ is internal storage path. After that i am creating two folders using below code:
val folder = File(Path)
if (!folder.exists()) {
if(!folder.mkdirs()){
Log.e("MyActivity","Folder not created")
}
}
I also tried using below code:
val folder = File(Path)
if (!folder.parentFile.exists()) {
if(!folder.parentFile.mkdirs()){
Log.e("MyActivity","Folder not created")
}
}
But still not working.
As per the getFilesDir() documentation, you should never assume a hardcoded installation or directory path - you should only be using relative paths compared to one of the storage directories

How could I create a file in the "Internal Storage" Directory?

I have a Samsung Galaxy S6. I'm currently working on a test application where I would like quick access to a folder with my files.
Using the provided "My Files" Application, it specifies that all those folders are in the "Internal Storage" folder.
I know that internal storage is private, but I want to create a folder in the default folder that windows accesses when the phone is plugged in.
For example, the following code does not create the directory in the correct location.
File storage = new File("/testappplication");
if(!storage.exists()) {
storage.mkdir();
System.out.println("Folder Created");
}
I just want to know the path where to create the folder. Many other applications have storage here, so I know its possible.
You can't create a directory inside the internal storage of the device. Except you've a root access for the app.
So, the following code won't work:
File storage = new File("/testappplication");
Because it tell the app to create a testappplication in the root folder.
You can only create the directory inside your app private folder within the following path:
String path = getFilesDir().getAbsolutePath();
And make the folder using the path.
Or you can use something like this:
File folder = new File(context.getFilesDir(), "testappplication");
if (!folder.exists()) {
folder.mkdirs();
} else {
// folder is exist.
}
Read more at Saving Files
First just for trial make runtime permmision and then try the following code
private void createInternalFile() {
File mediaStorageDir = new File(Environment.getExternalStorageDirectory()+"/"+getApplicationContext()
.getPackageName()+"/File/profile");
if (!mediaStorageDir.exists()) {
mediaStorageDir.mkdirs();
} }
then check your internal storage in which you will find a folder whose name is your package name
After a while later I found the answer to this question.
public void createDirectory() {
File file = new File(Environment.getExternalStorageDirectory(), "/test");
if (!file.exists()) {
file.mkdirs();
Log.w("DEBUG", "Created default directory.");
}
}
This is how you create it code wise. The reason it wasn't creating was due to Samsungs weird permissions.
Make sure you have the storage permission enabled in Settings -> Apps -> App Name -> Permissions. I needed to turn it on so it would create the folder.

Categories

Resources