How to create directory programatically in your app project? - android

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.

Related

Is it possible to create directories under Android/data/data/your_package/ files

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.

Creating a folder with file in Android's internal storage

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.

How to get access to application classes.dex file

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

Creating folder in the application directory

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));

open assets or raw file as File

Is there a way to open assets or raw resource as File ? I have method that needs open file as File but since i want store my file in assets or in raw folder i cannot access them.
I tried open it with file:///android_asset/filename.xml but received FileNotFound exception.
File file = new File("....");
x.load(file);
Is there a way to open assets or raw resource as File ?
No, sorry. They do not exist as files. You can only get an InputStream.
If you're willing to zip up the file first, you can do this:
File dir = getFilesDir();
IoUtils.extractZipResource(getResources().openRawResource(R.raw.texfilezip), dir, true);
File textFile = new File(dir, "textfile.txt");

Categories

Resources