I have a Nexus 9 device. I would create a personal folder in the /sdcard/ path, something like this:
/sdcard/MyFolder/
so i coded this:
File directory = new File("/sdcard/MyFolder/");
if(directory.mkdirs()){
Toast.makeText(context, "Folder created", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(context, "Folder not created", Toast.LENGTH_SHORT).show();
}
When I launch the app, it shows me "Folder not created" and so it doesn't create folder called MyFolder into /sdcard/ path. In Nexus 9 device the /storage/emulated/0path doesn't exist, so I have to use /sdcard/ path to accessing my storage.
I also used permission in my AndroidManifest.xml file, in this way:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Can you tell me please what's wrong?
Thanks
Since lots of devices have different file structures it is safer to use Environment.getExternalStorageDirectory() for determining the SD card path.
Can you try this code
File folder = new File(Environment.getExternalStorageDirectory() + "/MyFolder");
if (!folder.exists()) {
folder.mkdir();
}
try to specify external storage as listed below:
File directory = new File(Environment.getExternalStorageDirectory().toString()+"/MyFolder");
Related
I can't seem to "find" the path to this parent folder on my phone.
The first image is what Windows Explorer says my phone memory looks like.
The second image is what Total Commander says my phone memory looks like.
I have tried many combinations "Internal shared storage", with and without "storage", "0", "emulated", etc.
Try this:
File sdCard = Environment.getExternalStorageDirectory();
File directory = new File(sdCard.getAbsolutePath() + YOUR_DIR_NAME);
boolean dirOk = directory.mkdirs();
if (dirOk){
// dir created successfully
}else{
//dir create failed
}
In the manifest add:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
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 use the following to check if a directory exists and if it doesn't it then creates it
final String appPath = String.format("%s/DataFiles", Environment.getExternalStorageDirectory());
File f = new File(appPath);
if(f.exists() && f.isDirectory()){
Toast.makeText(getBaseContext(), "Exists", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getBaseContext(), "DOES NOT Exist", Toast.LENGTH_LONG).show();
f.mkdir();
This works fine on my S3 that has a sd card inserted
When I try it on anither phone that doesnt have an sd card in it recognises that the directory doesnt exists and then doesnt create the directory
Any ideas whats wrong
Mark
BTW I have tried inserting an sd card but doesn't make a difference it still doesn't create directory.
Before any operation you need to check if external storage is available.
http://developer.android.com/guide/topics/data/data-storage.html#filesExternal
if sd-card is not available or is not available for writing you need to use device storage:
http://developer.android.com/guide/topics/data/data-storage.html#filesInternal
I'm not trying to write into the external sd at /mnt/sdcard. I'm trying to create a folder for may app files and have them accesible by others.
I have an app called Libra that generates .csv files when exporting data and all af it goes to /Libra/ folder. I want my app to do the same.
As far as I've seen there's external storage which is the sd and internal which is a non public place for the app.
How can I make a dir at the root of the android file system as Libra does ?
If I ask for the external storage I get the following non desired locations :
Environment.getExternalStorageDirectory()
/storage/emulated/0/ (in my Nexus)
/mnt/sdcard (in the emulator)
If I try to make the folder in an absolute path /MyDiary It returns Error creating folder
File folder = new File("/MyDiary");
boolean success = true;
if (!folder.exists()) {
success = folder.mkdir();
}
if (success) {
Toast.makeText(getBaseContext(), "Already exists", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getBaseContext(), "Error creating directory", Toast.LENGTH_LONG).show();
return;
}
If I try to check if the Libra folder does exists, it says it doesn't exist:
File folder = new File("/Libra");
boolean success = true;
if (!folder.exists()) {
success = folder.mkdir();
}
if (success) {
Toast.makeText(getBaseContext(), "Libra exists", Toast.LENGTH_LONG).show();
}
Note: I've got the manifest permissions to write in external storage.
The root is a ramdisk. And the folder in it some are created by init.rc. Only init.rc have permission to create the folder in the ramdisk.
If you create the folder by init.rc, you still need init.rc help to mount a true stroage device on the new folder.
Repack like The init.rc file regenerated on restart
First of all you need root access Permissions to write.
You can see whether it has created folder there or not using rootexplorer app.
Try this after getting root access.
I don't think that's the right way to create a File in the Android OS.
From http://developer.android.com/training/basics/data-storage/files.html:
If you want to save public files on the external storage [I've read your advice, but I think this way you can access files from multiple apps, if that's what you want], use the getExternalStoragePublicDirectory() method to get a File representing the appropriate directory on the external storage. The method takes an argument specifying the type of file you want to save so that they can be logically organized with other public files, such as DIRECTORY_MUSIC or DIRECTORY_PICTURES. For example:
public File getAlbumStorageDir(String albumName) {
// Get the directory for the user's public pictures directory.
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), albumName);
if (!file.mkdirs()) {
Log.e(LOG_TAG, "Directory not created");
}
return file;
}
try first getting superuser permission to your app.Run linux commands for making folder and files into which you want to write data(You can do this with only linux commands)just at 1st run of your app.Then you can write data to files by getting FileOutputStream("filename") object.Leave ur remarks if it worked or not.
partial code for above procedures is
Process p=Runtime.getRuntime().exec("su");
OutputStream o=p.getOutputStream();
o.write("mkdir foldername\n".getBytes());
o.write("cd foldername\n".getBytes());
o.write("cat > filename\n".getBytes());
o.write("exit\n".getBytes());
o.flush();
p.waitFor();
I'm developing a simple application for Android devices.
I need to create a directory, but I can't do it.
File folder = new File(Environment.getExternalStorageDirectory ()+"/dir");
if(folder.mkdirs()){
CrearToast("Directorio creado"); //successfully created
}else{
CrearToast("fallo"); // error creating directory
}
*CrearToast creates a toast with the text in brackets.
I have set
uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
are you trying to write on SD card or phone memory??
Have you looked at this??
http://developer.android.com/guide/topics/data/data-storage.html
EDIT:
This is how i create my folders
File CheckDirectory;
CheckDirectory = new File(FolderPath);
if (!CheckDirectory.exists()){
CheckDirectory.mkdir();
}
SD card directory:
Environment.getExternalStorageDirectory().getAbsolutePath() + "/";