Where to place a xml file in android project? - java

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.

Related

where to store file in android project

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

android - retrieve all files from /res/raw folder as File type

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)

How to save files to a folder and query it within app, without the risk of losing them if they're renamed?

My app saves voice recording on external storage. The user inputs a filename after the recording, and I add that file name to arraylist and save that arraylist using the SharedPreferences.Now, if a user goes to the directory and renames the file, the name of the file will not change in the SharedPreferences. This is a problem because when the list of files is shown in my app, I open it by using the name saved in SharedPreferences. So if a file is renamed, that name will not be in the SharedPreferences, and will crash my app. Is there anyway I can avoid this, for example - can I save a file and retrieve it and open it irrespective of what it's name is? How would I do that?
I'm surprised by the lack of similar questions, which makes me feel I'm missing an obvious easier way of saving and retrieving files.
This gives you a list of all files within a specified directory:
File directory = new File("/path/to/directory/");
File[] files = directory.listFiles();
for( File file : files ) {
doStuffWithFile( file );
}
No need to know the file names, just the directory.

Java - Audio URL

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

Android internal storage reading file

I have a problem with Android internal storage. I have created folder in package root folder calling getDir() and with MODE_WORLD_WRITEABLE because I want camera app to write captured image in this folder. Anyway, I can see that captured image is inside that folder with DDMS.
Problem is that I cannot read that file.
I tried to read file with this code:
File file = context.getDir("images", Context.MODE_WORLD_READABLE);
File image = new File(file, "image.jpeg");
if (image.canRead())
Log.w("read", "can read");
else
Log.w("read", "can't read");
And in LogCat there is only second message (can't read).
I have also tried to create FileInputStream with file name but I receive FileNotFoundException.
Can somebody tell me what I'm doing wrong here?
Thanks in advance
EDIT:
Reading file is correct but only problem is that when cam app is saving image to specified location, permission set by cam app to image file are -rwxrwx---. After changing permissions with
adb (chmod 777 image.jpeg)
I was able to read image. Interesting thing is that cam app is writing images files to sdcard with ----rwxr-x.
Is there any way to change file permission in runtime?
Why not put it in the default photo directory?
File path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File file = new File(path, "DemoPicture.jpg");
( From: http://developer.android.com/reference/android/os/Environment.html )
See also a more complete example invoking the camera app: http://developer.android.com/training/camera/photobasics.html
Referencing to Android developers you should use openFileOutput() and openFileInput() to work on the internal storage... you are not allowed / not able to make dirs there. Android does it itself.. Those data will be cleared when your application will be deleted.

Categories

Resources