I have an issue with directory creation in Android. I use this code to create a directory if doesn't exist and then create a file under it.
dir=new File(Constants.TASK_DIRECTORY);
if(!dir.exists())
dir.mkdirs();
file=new File(dir, FILENAME);
file.createNewFile();
Sometimes it works fine, but sometimes when I check the folder from adb shell I see directories ending with 3 or more "|" characters. My directory name format is
"Abc_123-10.10.2000 ". What I see sometimes is exactly the same, but sometimes "Abc_123-10.10.2000|||" . I need to access the files under directories with the help of their name format but this situation makes it hard, any help would be appreciated.
I set TASK directory in this code
Constants.TASK_DIRECTORY=getFilesDir()+"/"+app.getUserName()+"-"+dt;
app is my application object
Edit: Solved this problem, it was because TASK_DIRECTORY was not properly set and contains "|" characters. But how can this be possible?
File can't store or save with some special character as below.
/\:?*"<>|
Related
I need to create 2 directories in my android application,
I can use this code to create a directory just fine
File m_Dir = new File(getFilesDir() + "/randomDir");
m_Dir.mkdir();
if (m_Dir.isDirectory()) {
Log.w("tag","randomDir dir is a directory");
}
however I cannot use this because it does not create a directory, no errors, but it just isn't a directory for some reason, even though the only thing that changed is the name of the file
File m_Dir = new File(getFilesDir() + "/dict");
m_Dir.mkdir();
if (m_Dir.isDirectory()) {
Log.w("tag","dict dir is a directory");
}
note they must be named "dict and "dbfiles", Using them for JWI wordnet, and thats how it locates the files it needs.
I have tried this many times with different names, and the only reason that this m_Dir.isDirectory returns false is because of the name, this doesn't make sense to me. The only reason I can think of, that this might fail is because I have file directories in my Assets folder with the same name.(I am copying these into my new directory so i can use them). I can change the name of the Asset folders (Probably what I'm going to do). However I created another folder in assets using a different name, I tried that name and it worked creating the directory using the same code as before. So that may not even be the problem. I was wondering wondering why this problem is occurring (No errors but no directory) and if there is any better way around it?
I'm goig to be mad with a strange issue. If i create a folder inside my code as
directory_path = Environment.getExternalStorageDirectory()
+ "/" + context.getResources().getString(R.string.app_name);
directory = new File(directory_path);
if (!directory.exists()) {
directory.mkdirs();
}
a new folder is created inside /sdcard/ . If i try to print on logcat directory_path variable, the path is different: /storage/emulated/0/
and if i go to that path, i found another folder with the same name of the one created on /sdcard/ . This is a problem for me because when i try to write some data into that folder, everithing goes in the one on /storage/emulated/0 , and the other one (that is the folder i want use) remain empty.
Why?
Have you tried reading back the data? /storage/emulated/0/ is the new path introduced in JB to support multiple users on tablet. But as long as you access external files using Environment.getExternalStorageDirectory() it doesn't really matter where they really reside.
Here's some additional info: https://android.stackexchange.com/questions/35541/why-did-sdcard-turn-into-sdcard-0-with-4-2
/storage/emulated/0/: to my knowledge, this refers to the "emulated
MMC" ("owner part"). Usually this is the internal one. The "0" stands
for the user here, "0" is the first user aka device-owner. If you
create additional users, this number will increment for each.
/storage/emulated/legacy/ as before, but pointing to the part of the
currently working user (for the owner, this would be a symlink to
/storage/emulated/0/). So this path should bring every user to his
"part".
/sdcard/: According to a comment by Shywim, this is a symlink to...
/mnt/sdcard (Android < 4.0)
/storage/sdcard0 (Android 4.0+)
For more detail you can visit stackexchange
My application is mostly c++ (using NDK) so I use fopen, fwrite, etc. standard functions to create and game save files and write into them.
When I use fopen("game.sav", "wb"), it appears that it's being created at path
/data/user/10/com.my.game/files/game.sav.
My app is multi-user. So I want to have a separated folders where users store their save-files. And instead of the path above I'd like to have paths like
/data/user/10/com.my.game/files/user0/game.sav,
/data/user/10/com.my.game/files/user1/game.sav, etc
My app's frontend is in Java, and when new user is being registered, I want to create a folder /data/user/10/com.my.game/files/user0/. But I don't know how to do it, because
final File newDir = context.getDir("user0", Context.MODE_PRIVATE);
results in path being created at /data/user/10/com.my.game/app_user0 that's a different path.
It is possible to create folders at /data/user/10/com.my.game/files/ and how ?
Simple way to do it, this code you can change it suit many conditions. If you know that your path is different from what getFilesDir() gets you then you can create a File first of all by using a path that you know and the last 2 lines of code will still be same.
File file = this.getFilesDir(); // this will get you internal directory path
Log.d("BLA BLA", file.getAbsolutePath());
File newfile = new File(file.getAbsolutePath() + "/foo"); // foo is the directory 2 create
newfile.mkdir();
And if you know the path to "files" directory:
File newfile2 = new File("/data/data/com.example.stackoverflow/files" + "/foo2");
newfile2.mkdir();
Both code works.
Proof of Working:
As the title suggests, I am trying to create a folder on Android, but all of the slashes have been removed from it.
For some more background information:
Specifically, I am trying to create a directory to store my application's users' files. These files must be accessible to the user from a file manager (such as File Manager HD) because the application does not support full file management. Using the standard from API level 8+, I reference the root of the publicly accessible folder with Environment.getExternalStoragePublicDirectory(). I then try to create a folder located at DCIM > Sketchbook > [the name of the sketch] using File.mkdirs(). For more information, see the code below.
I have already:
checked to make sure that the SD card is mounted, readable, and writable
enabled the permission WRITE_EXTERNAL_STORAGE
tried using File.mkdir() for every file in the hierarchy up to the folder location
tried using /, \\, File.separatorChar, and File.separator as folder separators
Code:
boolean success = true;
//The public directory
File publicDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
//The location of the sketchbook
File sketchbookLoc = new File(publicDir + "Sketchbook" + File.separator);
//The location of the sketch
//getGlobalState().getSketchName() returns the name of the sketch: "sketch"
File sketchLoc = new File(sketchbookLoc + getGlobalState().getSketchName() + File.separator);
if(!sketchLoc.mkdirs()) success = false;
//Notify the user of whether or not the sketch has been saved properly
if(success)
((TextView) findViewById(R.id.message)).setText(getResources().getText(R.string.sketch_saved));
else
((TextView) findViewById(R.id.message)).setText(getResources().getText(R.string.sketch_save_failure));
With various incarnations of the aforementioned tests (the ones that actually worked), I have received a consistent result: I get a new folder in DCIM whose name corresponds to the combination of all of the folders that should have been hierarchical parents of it. In other words, I have created a new directory, but all of the folder separators have been removed from it.
Now, I ask you:
Am I attempting to save the user data in the correct location? Is there another way that I should be doing this?
Is it even possible to create new folders in the DCIM folder? Does Android prevent it?
Is this problem specific to me? Is anyone else able to create a folder in the DCIM folder?
Am I using the right folder separators?
Is there something else that I am absolutely, completely, and utterly missing?
Now that I am done typing, and you are done reading my (excessively long) question, I hope that I can find some sort of answer. If you need clarification or more information, please say so.
EDIT: An example of the created folder is "DCIMSketchbooksketch", where it should be "DCIM/Sketchbook/sketch".
don't use
File sketchbookLoc = new File(publicDir + "Sketchbook" + File.separator);
but
File sketchbookLoc = new File(publicDir , "Sketchbook");
because publicDir.toString() will not end with a file separator (even if you declared it that way). toString() gives the canonical name of the file.
So your source becomes :
//The location of the sketchbook
File sketchbookLoc = new File(publicDir , "Sketchbook" );
//The location of the sketch
File sketchLoc = new File(sketchbookLoc , getGlobalState().getSketchName() );
I want to read text file, that was written by other my app. It's saved ad "Android/data/MyPackageName/files/"
I use this code:
File file = new File("//Android//data//MyPackageName//files//", "filename.txt");
FileInputStream is = new FileInputStream(file);
but i get exception "no such file or directory"
I am sure that solution is pretty simple, but i can't find it yet.
Thank you for your help!
I don't think it is right to use the double backslash "//", one is enough. Also, the path should be "/mnt/sdcard/Android/data....". I am not sure the "/mnt/sdcard" is applicable on every device, so my suggestion is to use Environment.getExternalStorageDirectory to get the root dir on sd card.