Android: Trying to make directory, makes file instead - android

I'm trying to create an empty directory, but instead it creates a file.
publicDocsPath =
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
autoTestDir = new File(publicDocsPath + File.separator+"autoTest");
autoTestDir.mkdirs();
I can get around it by creating a dummy file under neath it, as such
autoTestDir = new File(publicDocsPath + File.separator+"autoTest" + File.separator+ "nullfile");
But I would like to know if I'm doing something wrong or if there is a way to tell the system you want to create a directory not a file.
Note: I'm on a mac, and I'm using Android File Transfer program to verify my results. Maybe the file is being created as a directory, but issue is with Android File Transfer and it shows the directory as a file.

Try this
File documents = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
File dir = new File(documents.getAbsolutePath() + File.separator + "autoTest" + File.separator);
// creates if doesn't exists
dir.mkdir();
You have to add the file separator after "autoTest".

Related

How to create a sub directory inside a private directory in android

I have created a directory like this:
File mydir = context.getDir("my_private_dir", Context.MODE_PRIVATE);
Now I want to create another directory inside my_private_dirfor which I've tried doing it this way:
File file = new File(mydir.getAbsolutePath()+ File.separator + date);
if (file.mkdir())
Log.d(context.getClass().getSimpleName()," date dir created");
I don't know why this is not working out. The subdirectory is not getting created at all.
I want all the directories to be private and only my app should be able to access it. SO I thought to make the parent directory private and to create subdirectories inside it normally will be enough. But I'm not able to understand why the subdirectory is not getting created.
Can anyone please help me with this problem?
Thanks in advance :)
Regards
Try to use mkdirs() instead of mkdir()
File file = new File(mydir.getAbsolutePath()+ File.separator + date);
if (file.mkdirs())
Log.d(context.getClass().getSimpleName()," date dir created");

IOException: No Such File or Directory (Android)

This is my first Android app and my first attempt at writing something to file. I'm trying to capture a log according to these instructions and I'm getting the FileNotFoundExeption ENOENT (No such file or directory). That's fair enough because the directory does not exist. But then how do I create the directory? Or use another one? I don't know best practices for where to write logs to email them, nor do I know how to make a new directory for them.
This is the path I'm trying to use.
String path = Environment.getExternalStorageDirectory() + "/" + "MyFirstApp/";
String fullName = path + "mylog";
File file = new File (fullName);
The parent dir doesn't exist yet, you must create the parent first before creating the file
String path = Environment.getExternalStorageDirectory() + "/" + "MyFirstApp/";
// Create the parent path
File dir = new File(path);
if (!dir.exists()) {
dir.mkdirs();
}
String fullName = path + "mylog";
File file = new File (fullName);
Edit:
Thanks to Jonathans answer, this code sample is more correct. It uses the exists() method.
You also need the permission in your manifest:
<manifest ...>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
...
</manifest>
I'd like to add to Francesco's answer, that instead of asking if it's a directory, you could ask if it exists with dir.exists() method.
And also check that you've set the proper permissions in the Manifest file.
Hope it helps
Jonatan

android data save directories

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:

Removing "app_" prefix when creating subdirectories in application's internal storage

Android provides many options for storing data persistently on the device. I have opted for internal storage, so please don't make suggestions for storing on an SD card. (I've seen too many questions about internal storage have answers for SD cards!!)
I would like to create subdirectories in my application's internal storage directory. I followed this SO answer, reproduced below.
File mydir = context.getDir("mydir", Context.MODE_PRIVATE); //Creating an internal dir;
File fileWithinMyDir = new File(mydir, "myfile"); //Getting a file within the dir.
FileOutputStream out = new FileOutputStream(fileWithinMyDir); //Use the stream as usual to write into the file.
Unfortunately, that's creating subdirectories with "app_" prefixes. So, from the example, the subdirectory looks like "app_mydir" (not ideal).
The answer to this SO question suggests that you can get rid of the "app_" prefix this way:
m_applicationDir = new File(this.getFilesDir() + "");
m_picturesDir = new File(m_applicationDir + "/mydir");
But I want to write a zip to something like /data/data/com.mypackages/files/mydir/the.zip.
So, in the end, my code looks like this:
File appDir = new File(getApplicationContext().getFilesDir() + "");
File subDir = new File(appDir + "/subDir");
File outFile = new File(subDir, "/creative.zip");
But, this is creating another error: "File does not exist" when I try this:
FileOutputStream fileStream = new FileOutputStream(outFile);
How can I (1) create a subdirectory without the "app_" prefix and (2) write a zip into it?
Also, if my first demand isn't reasonable, tell me why in the comments! I'm sure "app_" prefix has some meaning that escapes me atm.
Did you create the directories?
File appDir = getApplicationContext().getFilesDir();
File subDir = new File(appDir, "subDir");
if( !subDir.exists() )
subDir.mkdir();
File outFile = new File(subDir, "creative.zip");
Note that you shouldn't use / anywhere in your app, just in case it changes. If you do want to create your own paths, use File.separator.

Saving data on external storage

I want to save some data in the user's external directory (ie. SD card), but there seems to be a weird problem. I'm using Environment.getExternalStorageDirectory() which returns "mnt/sdcard/" (which is fine). I want to create two folders on in this directory so I do:
File main = new File(getExternalStorageDirectory() + "/my_app/some_data");
if(!main.isDirectory())
main.mkdirs();
Now I thought this would make the directory "mnt/sdcard/my_app/some_data", but after using a file manager to look at the SD card, it turns out that this folder is created at "mnt/sdcard/my_app/mnt/sdcard/my_app/some_data", which is quite bizarre. Can anyone tell me how to fix this?
Try the following and see what you get...
String packageName = this.getPackageName();
File myFilesDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "Android" + File.separator + "data" + File.separator + packageName + File.separator + "files");
myFilesDir.mkdirs();
It's exacly what I use to create a working directory on an SD card. For me it creates...
/mnt/sdcard/Android/data/com.mycompany.myApp/files
...where 'com.mycompany.myApp' is the actual package name of my app.

Categories

Resources