I want to store a .json file within the res directory. How should the path be defined?
FileWriter file = new FileWriter("path_here");
You can't! Resources are read-only at runtime.
Related
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 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));
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");
I have a problem.
I use this code to read the content of a zip file:
File file = new File(TogglesManager.EXTERNAL_STORAGE_THEMES_DIRECTORY+"filename.zip");
ZipFile zip = new ZipFile(file);
But when I try to use an assets I have a FileNotFoundException.
To read asset zip file I use this code:
File file = c.getFileStreamPath("assetsFile.zip");
ZipFile zip = new ZipFile(file);
The "file" is not null, because if I write file.getName(); I see the correct file name.
I don't want to use ZipInputStream class, but only ZipFile
You cannot access Assets using normal file operations. You will have to use
AssetManager assetManager = mContext.getAssets();
InputStream is = assetManager.open("assetsFile.zip");
But since ZipFile does not take InputStream as a parameter, you will have to copy asset file onto internal storage and then use File
Further AFAIK, apk is zipped, so storing a zipped file inside assets might not change storage used by much, so maybe you donot need to use zipfile inside assets.
I'm trying to get the names of my folders in "assets". I can get the names of the files with an AssetManager by using the method assetManager.list(). But the problem is that it return only files' name and not folders' names. So I'm trying to use the listFiles() method but i can't access to the Assets directory; I've try the following :
File dir = new File ("file:///android_asset/");
File[] files= dir.listFiles();
But it doesn't work :( ... Is there a way to get the folders' names contained in the Assets directory ?
The URL "file:///android_asset/" doesn't point to a particular directory, it is only used by WebView to address assets. Assets are read-only and defined at compile time, so it's assumed their directory structure is known to the programmer. It's inconvenient, but that's the way it's designed