i have used getFilesDir() to create a folder in the application directory, it gives the path of the applicatoin directory as follows
/data/data/{my application package}/files
but when i use it to create a new folder using
File folder = new File(getFilesDir()
+ "/MyFolder");
if (!folder.exists()) {
folder.mkdir();
}
i don't see any folder. Also when i access in ES Explorer the actual path of the application directory is
/Android/data/{my package name}/files
My question is how to create a folder in the application directory so that it can be deleted automatically on application uninstallation.
Use method Context.getDir() instead. You don't need to invoke mkdirs(), because getDir() will do it automatically.
Quote from the documentation:
Retrieve, creating if needed, a new directory in which the application
can place its own custom data files. You can use the returned File
object to create and access files in this directory. Note that files
created through a File object will only be accessible by your own
application; you can only set the mode of the entire directory, not of
individual files.
Use this by making a use of getDir()
File dir = ctx.getDir("my_sub_dir", Context.MODE_PRIVATE);
File newfile = new File(topDirFile.getAbsolutePath() + File.separator + "new_file_name");
newfile.createNewFile();
BufferedOutputStream fout = new BufferedOutputStream(new FileOutputStream(newfile));
Related
I would like to save some files in different directories under Android/data/data/my_app_package/files directory. I am able to obtain the path to the "files" directory by calling context.getExternalFilesDir(null), but can't create a directory under the path returned. Does anyone know why?
Thanks,
File root = SoundApplication.getAppContext().getExternalFilesDir(null);
File soundDir = new File(root, "/sound-dir");
if (!soundDir.exists()) {
boolean dir = soundDir.mkdirs();
System.out.println("dir = " + dir);
}
soundDir.mkdirs() returned true, but I don't see "sound-dir" in Android.data.mayPackage.files directory with Windows explorer.
I want to create directory in my android project. I have tried below code I could not find any directory in my project.
File myDir = new File(getCacheDir(), "files");
myDir.mkdir();
if(!myDir.exists())
myDir.mkdirs();
If you just want to create a Dir in your internal storage you can do it in the below method
File mydir = context.getDir("files", Context.MODE_PRIVATE); //Creating an internal dir;
if (!mydir.exists())
{
mydir.mkdirs();
}
getFilesDir() returns a File object to a directory that is private to your application only.
getDir() enables you to create any file or directory in the internal memory, which is also accessible by other applications depending on the mode you create it.
getCacheDir() returns the absolute path to the application specific cache directory on the filesystem but doesn't enable you to create Dir there. You can create a file though.
Please note that this file will be stored in internal memory and can be cleared by system as well as user manually on clearing cache.
i'm trying to create a folder with a .csv file inside in android internal storage, but the folder doesn't appear in the regular file explorers.
The code is the following.
private String FOLDER_NAME = "app_folder";
File folder = new File(Environment.getDataDirectory().getAbsolutePath() + File.separator + FOLDER_NAME);
if( !folder.exists() )
folder.mkdir();
but the folder doesn't appear in the regular file explorers.
That is because internal storage cannot be viewed by any sort of file explorer, except on emulators or rooted devices.
File explorers — on-device and desktop — usually examine external storage.
I believe that every application can get access to its own files, for example - to classes.dex file.
I wonder how the application get access to its files?
and another question, can I fake this access and get access to another application.?
you can point to the apk file of your application using ApplicationInfo.publicSourceDir
File yourApk = new File(this.getApplicationInfo().publicSourceDir);
//create the destination dir
File tempDir = new File(getFilesDir(), "temp_dir");
//and then unzip the apk to that dir
unzip(yourApk , tempDir);
//now the tempDir will contain all the resources including the dex file and its accessible
this file is a zip file you can decompress that file using this link
How to unzip files programmatically in Android?
so you will have the access to all resources plus the classes.dex
hope this will help
I am writing to a path mnt/sdcard/foldername. I get no errors and the file is written, but when I browse the internal storage of my phone I can not find the folder. Where would the folder be located? I have a galaxy nexus?
Code is here:
File sdCard = Environment.getExternalStorageDirectory();
File directory = new File (sdCard.getAbsolutePath() + "/statReporter/");
directory.mkdirs();
Log.d("Tag", directory.toString());
//Path and name of XML file
File file = new File(directory, appendTimeStamp("phoneNum") + ".csv");
FileOutputStream fOut = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
//Write to CSV File
osw.write(message);
osw.write(',');
//Flush and close OutPutStreamWriter and close FileOutputStream
osw.flush();
osw.close();
fOut.close();
Log.d("File Logger:", "File write successfully!");
Cant find it in Windows Computer\Galaxy Nexus\Internal Storage but all other folders appear.
I used an app called OI File Managaer and I can view the folder and file on phone but how do I view it through Windows OS?
If you really want to find files on your device, I'd recommend one of the Google Play apps you can find (I personally like ASTRO File Manager) or, from the PC you can use (for instance) Android Commander (which, incidentally, will let you use the same file path structure you'll be using from within your developing environment).
I believe that Android devices will not actually show you all paths and available files when you browse it, for instance, with Windows Explorer.
If you're using Eclipse, in the DDMS perspective you can browse your device's file structure. On your right you have the "File Explorer" and as far as I remember, it's a pretty complete tool.
Don't use /mnt/sdcard for accessing external storage. You can use Environment.getExternalStorageDirectory() to access path to external storage. This may vary from device to device.