I have a file with some data that I want only my application to have access to.
I was wondering in which folder I should put the file in my android project in order to have access to it using
File file = new File(context.getFilesDir(), filename);
Thanks
This is just an empty folder in Android device.
To access your files in assets
InputStream is = getAssets().open("subfolder/anyfile.txt");
to access files inside raw use
InputStream XmlFileInputStream = getResources().openRawResource(R.raw.somesrc);
getFilesDir() is just a folder that uses for openFileOutput(String, int) method. More detailed: https://stackoverflow.com/a/21230946/1979882
Use Assets Folder For files that will be bundled with the application and will be stored inside the apk as is. you can add an "asset" folder to your project by right clicking your app in the project explorer then select
New=> Folder=> Assets Folder.
you can open the files stored in the assets folder using the assets manager for example to open an image kept in /assets/img/image1.png:
InputStream is = assetManager.open("img/image1.png");
for HTML files you can load the file by its URL :
webView.loadUrl("file:///android_asset/"+name);
Here you can get a clear functionality of saving,retrieving a file
Here is the Link:http://www.mysamplecode.com/2012/06/android-internal-external-storage.html
Related
How can I retrieve all the files in /res/raw folder as File?
I have found this solution, and "New Folder" value I have replaced with "res/raw" as you can see in the following code:
public File[] getRawFiles() {
File sdCardRoot = Environment.getExternalStorageDirectory();
File yourDir = new File(sdCardRoot, "res/raw");
return yourDir.listFiles();
}
When program is started, I get an exception on line return yourDir.listFiles:
Caused by: java.lang.NullPointerException: Attempt to get length of null array
How can I fix this, and what is correct path to "res/raw/" ?
How can I retrieve all the files in /res/raw folder as File?
You cannot do this. As we discussed yesterday, resources are not files on the filesystem of the device. They are files on your development machine. They are merely entries in an APK file on the device.
Either:
Do whatever you are trying to do some other way that does not involve files, or
Use openInputStream() on a Resources object (you can get one from any Context via getResources()), and use Java I/O to copy the contents of a resource to a local file, such as on internal storage, and using reflection to iterate over the actual resources, or
Switch to using assets/ instead of res/raw/, as AssetManager allows you to list() assets (though you still only get an InputStream on an asset, as like resources, assets are not files on the device)
I have a audio file stored in a folder within the project and I'm trying to call the sound file within the folder.
I am able to call the file using the full URL
new java.net.URL("file:C:\NetBeansProjects\Puzzle\audio\applause2.wav"));
But is there a way to shorten it so that if I move the project I don't require changing the code?
Thanks
File file = new File("audio\applause2.wav");
Assuming you keep the audio folder at the same level relative to the jar
I've just a finished a program which makes use of lots of audio and image files. These are included in the project in src in the folder audio and images respectively. When I reference them I use use:
ImageIO.read(aclass.class.getResource(images/animage.png));
for images (referencing images directly)
or
FileInputStream fis = new FileInputStream(src/audio/audio.wav);
for audio (reference audio from the src file)
However, when I clean and build the project and then try and run it as a jar most of assets are gone. Some of the images remain but I lose all audio.
Is there a way I can make sure that all these assets get loaded when I just run the exported jar?
to load the resources inside the jar file you have to use the YourClass.class.getResourceAsStream method. it will return an input stream. should be self explainatory
For my project I am using an xml file to fetch information. I know this can be stored inside assets folders or inside resources/raw folder. My issue is I have to modify the xml dom and write back to the file again as a persistent storage.
I have read that I cant write back to a file in assets folder. But no idea regarding the file in resource folder?
Where can I keep the file for this purpose? Is storing the file in sdcard the only possible option?
Thanks
I would include the file in res/raw and then create a local copy in the file directory for your app. On app init, you can check to see if it exists in the external storage and then if it does not, load it anew from res/raw.
Without going too far into the nitty-gritty, something like this perhaps:
File filesDir = context.getFilesDir();
File xml = new File(filesDir, YOUR_FILE_NAME);
if(xml.exists() {
/* load data from filesDir */
} else {
/* load data from res/raw and save to filesDir */
}
Is storing the file in sdcard the only possible option?
Yes, and you can also store the file in internal memory.
You do not have the permission to modify the apk files.
I am using eclipse and I use the following code to load my image from a folder.
getClass().getResource("/images/image.jpg").getFile())
The image folder is located inside the bin folder in the project folder. It works fine when loading in eclipse, but when I export it to a jar it does not load. I have tried puting the image folder in all possible places in the jar, but it does not.
How do I load an image folder in a jar?
You can use getResourceAsStream() method instead to get InputStream instance with your file data.
UPDATE: Loading of files from a jar happens with the help of class loader. And it can give you instance of InputStream (not FileInputStream) of any internal resource (be it image file or sound file or text file). File writing shouldn't work inside jar.
Use this. This method doesn't return immediately.
public Image getImage(String img){
return new ImageIcon(getClass().getResource(img)).getImage();
}
If you want to load from the root of the JAR, then
return new ImageIcon(getClass().getClassLoader().getResources(img)).getImage();