The below code doesn't create a folder in my device.
String intStorageDirectory = context.getFilesDir().toString();
File folder = new File(intStorageDirectory, "test");
folder.createNewFile();;
I need a folder created for my app to store media, when user installs it. That folder should be visible on file explorer. How can i do it?
With the current snippet you created a file, you can also create folder by creating file but your current directory is the base folder, getFilesDir() points internal storage for your app which not visible nor accessible unless explicitly declared. You can create a folder and file by creating with new File().createNewFile() or create only folder using mkdirs() but you won't be able to display it using a file explorer app and that folder and files inside it will be deleted when/if user uninstalls your app.
To save files externally(This doesn't mean saving to SD Card) you can create directory and file with
File mediaStorageDir = new File(Environment.getExternalStorageDirectory(), folderName);
if (!mediaStorageDir.exists()) {
mediaStorageDir.mkdirs()
}
File mediaFile = new File(mediaStorageDir.getAbsolutePath() + File.separator + fileName);
And you need some kind of OutputStream to write data to that file.
Make sure that you ask <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> inside your AndroidManifest.xml file and ask write permission on runtime if your android:targetSdkVersion="23" or above
new File(context.getFielsDir(), "test").mkdirs();
createNewFile creates a file, not a folder. Using mkdirs instead of mkdir ensures that all parents exist. There's also no reason to go through a string when you already have a File.
Adding folder.mkdirs(); should work in place of folder.createNewFile(); And don't forget to add the permissions.
This will create a folder in you data directory.
And just a suggestion , if you want to store media in a SD card folder maybe Environment.getExternalStorageDirectory() is good.
Related
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.
I'm developping an application where I need to create new folders/files in the sdcard. The thing is I can see them using a root explorer but not with the default one which comes with Android.
I've taken a look at several similar questions here but don't seem to work for me.
For sure, I'm using in my manifest this:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
and I'm writing all the Files using:
String path = Environment.getExternalStorageDirectory() + "/FolderName/FileName.format"
but as I said, these new folders/files remained hidden and can only be seen using a root explorer. Neither the folder nor file name starts with "."
Thanks in advance.
Try using mkDirs()
File dir = new File(fullPath);
if (!dir.exists()) {
dir.mkdirs();
}
I am developing an android application. I need to create a folder in the internal memory, but when I try to create the folder I get the error below. I am running in an emulator.
mkdir failed for /mnt/New Folder , read only file system
I have tried many paths, but still the error persists. The only folder that I am able to create is called "cache", but I cannot browse it by my file chooser activity.
Any idea where is the suitable place to create folders without any permissions?
You can achieve it by this from a Context object (like Activity).
File files_folder = getFilesDir();
File files_child = new File(files_folder, "files_child");
files_child.mkdirs();
File created_folder = getDir("custom", MODE_PRIVATE);
File f1_child = new File(created_folder, "custom_child");
f1_child.mkdirs();
The function
getFilesDir()
will get the folder data/data/yourpackagename/files in internal memory. And the function
getDir("custom", MODE_PRIVATE)
will create a folder name app_custom in your app internal folder.
Answered by Minhtdh
I guess what you call internal memory is atualy the external memory (which can be open by
file chooser activity, the real internal memory only can be open if you have rooted)
If that true, you should chek those belows:
- first, you will need the write storeage permission in Manisfest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
- then you should use `
String path =
Environment.getExternalStorageDirectory().getAbsolutePath() +
"/yourfoldername"
`
than
mnt/yourfoldername
at last you should use mkdirs to create folder than mkdir
how to create folder with my application name on android internal memory not on the SDcard and create on it files like whatsapp and viber any ideas ?
Thanks
File storagePath = new File(Environment.getExternalStorageDirectory()+ "/foldername");
storagePath.mkdirs();
don't forget to add permission in manifest file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
You can use for example:
private MainActivity activity;
[...]
File fileDir = this.activity.getFilesDir();
I used this solution in an AsynTask. You can also use context.
getFilesDir()
Returns the absolute path to the directory on the filesystem where files created with openFileOutput(String, int) are stored.
Returns the path of the directory holding application files.
From my application, I want to store some images into my SD card. For that I need to create a one folder.
At the first time folder will create but after it checks whether that folder is present or not. How can I do it?
below code will create a directory if it does not exist
File direct = new File(Environment.getExternalStorageDirectory() + "/New Folder");
if(!direct.exists())
{
if(direct.mkdir())
{
//directory is created;
}
}
You should request the following permission first in your Android manifest :
android.permission.WRITE_EXTERNAL_STORAGE
and execute above code by Rasel for it to work.