embedded Android: xml file is empty after board shutdown - java

I'm working on an embedded android project and I'm facing a weird problem.
I'm saving values on xml config files (using the transform class). I'm sure that the save operation finished (but I have to handle the save not finished case later).
if I shutdown the board in the screen where the files are saved. After the next startup, the config files are empty!!!!
I tried FileWriter and FileOutputStream instead of the transform class (I, of course, closed the stream after use) but I'm facing the same problem.
I tried to use a backup file to avoid this issue: I write into a new file, then delete the original one and renamed the new one with the original name. this solution doesn't fix the problem!!
I tried another solution: I created a backup file. I saved into this one, and then I copied this one into the original one (without any deleting). after my test case, the two files are empty!!!!
I's really a weird problem!! does anyone faced the same issue on android or on another os? I saw that question too content of XML file created by java app dissapears if computer is shut down and I think it's a the same issue.
Is this a java issue? any solutions?
Thank you for your help
Regards

I didn't find the root cause. I think it's mostly related to the board than android. I find a turn around, I created the backup files into the private storage space /data/data/my.package.name (which is located in the Nand flash in my case) and when my application started, if the config file is empty, I use the backup file from the private storage.
Really a weird problem but my issue is solved.
Cheers

Related

how do i go about doing this file path?

I made an app that i'm am working on updates for and in the old version I hard coded the file path like this
"/data/data/my.app.here/troll"
so now I am trying to do an update and I am trying to use something similar to
Context.getFilesDir()
but that returns
/data/user/0/my.app.here/files
the app info is stored on the troll file and when I push updates I don't want to use another path and have them start their information all over again. What's the best way to resolve this situation?
Can someone kindly tell me the best way to resolve this situation?
Call getParentFile() on the File returned by getFilesDir() to get its parent directory. Your troll file should be in there. That wasn't a great place to put a file, and so YMMV.
If your concern is the /data/user/0/, that will be handled by Android and should map to /data/data/ when the primary user is the one running your app.

Putting file to S3 right after it's created

I have two machines with different Java applications that both run on Linux and use a common Windows share folder. One app is triggering another to generate a specific file (e.g. image/pdf). Then the first app tries to upload the generated file to S3. The problem is I sometimes get this:
com.amazonaws.services.s3.model.AmazonS3Exception: The Content-MD5 you specified did not match what we received.
OR this:
com.amazonaws.AmazonClientException: Data read has a different length than the expected: dataLength=247898; expectedLength=262062; includeSkipped=false; in.getClass()=class com.amazonaws.internal.ResettableInputStream; markedSupported=true; marked=0; resetSinceLastMarked=false; markCount=1; resetCount=0
All the processes are happening synchronously, one after another (i have also checked the logs which show no concurrent activity). Also I am not setting the md5 hash or the content length by myself, aws-sdk handles it by itself.
So my guess is that the generating application has written a file and returned but in fact it is still being written by the OS in background and that is why the first app is getting an incomplete file.
I would really appreciate suggestions on how to handle such situations. Maybe there is a way to detect if the file is not currently being modified by the OS?
I was experiencing AmazonS3Exception: The Content-MD5 you specified did not match what we received. I finally solved it by addressing the first item on the list below, not terribly obvious.
Possible Solutions For Anyone Else:
Make sure not to use the same ObjectMetadata object across multiple putObject calls.
Consider disabling ChunkedEncoding. client.setS3ClientOptions(S3ClientOptions.builder().disableChunkedEncoding().build())
Make sure the file isn't being edited while it's being uploaded.

writing and reading input to xml

I am new to Android development and have been struggling with a problem. I have been trying to learn how to save user inputs to file. I have asked a similar answer and ended up figuring out a solution although it didn't work out the way I want. I have been searching high and low about how to write files to xml. I would to have a user be able to save, add, retreive, and be able to remove items from an XML file. I think the problem I am having as far as learning this is I'm not sure what the processes are call to properly look for a solution. This isn't for school work, I'm not looking for anyone to write up some code for me. What I am hoping for is proper terminology, maybe a link to some helpful information and such. I believe this process is called parsing, and you can add to XML through appending, not sure about much after this. Thank you in advance to anyone who can offer assistance.
To have the XML always available for the app you should put it in the "assets" folder. You should copy-paste the xml file to modify it, to ensure the file won't be corrupted afterwards.
First you have to copy the xml to the sdcard (preferably to a folder with your app name as folder name), you can use this to learn how to copy a file. After you have done that you can use the JDOM Parser to modify that xml file.
Put the xml after the modification back in the assets folder.

best way to check for new xml files in java

i am writing a program that parses xml files that hold tourist attractions for cities. each city has it's own xml and the nodes have info like cost, address etc... i want to have a thread on a timer to check for new xml files or more recent versions of existing ones in a specific directory. creating the thread is not the problem. i just have no idea what the best way to check for these new files or changed files is. does anyone have any suggestions as to an easy way to make do that. i was thinking of crating a csv file with names and date altered info for each file processed and then checking against this csv file when i go to check for new or altered xml, but that seems overly complicated and i would like a better solution. i have no code to offer at this point for this mechanism i am just looking for a direction to go in.
the idea is as i get xml's for different cities fitting the schema that it will update my db automatically next time the program runs or periodically if already running.
To avoid polling you should watch the directory containing the xml file. Oracle has an extensive documentation about the topic at Watching a Directory for Changes
What you are describing looks like asynchronous feeding of new info. One common pitfall on such problem is race condition : what happens if you are trying to read a file while it's being modified or if something else tries to write a file while you are reading it ? What happens if your app (or the app that edit your xml files) breaks in the middle of processing ? To avoid such problems you should move files (change name or directory) to follow their status because moves are atomical operation on normal file systems. If you want a bullet proof solution, you should have :
files being edited or transfered by an external part
files being fully edited or transfered and ready to be read by you app
files being processed
files completely processed
files containing errors (tried to process them but could not complete processing)
The 2 first are under external responsability (you just define an interface contract), the 2 latter are under yours. The cost if 4 or 5 directories (if you choose that solution), the gains are :
if there is any problem while editing-tranfering a xml file, the external app just have to restart its operation
if a file can't be processed (syntax error, oversized, ...) it is put apart for further analysis but does not prevent processing of other files
you only have to watch almost empty directories
if your app breaks in the middle of processing a file, at next start it can restart its processing.

Check if the folder and all data already exist

My Android application downloads data only the first lunch. the data is ~50 mb with ~2500 files.
1. Is it a good idea to store if the files got downloaded in SharedSettings? The problem is that if a user clears the data application (maybe by mistake), he has to redownload everything. I manually copy a prepacked database to /data/data/../databases/, is it a good idea to check if the db exists, and if no then download everything?:
if(new File(/data/data/../databases/myDB.db).exists){//dont download}
2.Is getting the folder size and checking if its the same a good way to see if the folder+data are good? or is there a better way to check if 2 folders are the same?
Thanks.
No, do not put 50MB of data into SharedSettings. That will fall over and die. A set of SharedSettings is stored in XML on disk and entirely loaded into RAM when opened. This also won't keep the user from clearing this data.
For determining whether the data has been downloaded, I would suggest just having a file you make once the download is complete indicating it is done. The user can't selectively remove files. They can clear your data, but that will also clear the sentinel file and you will know you need to re-download. (Also keep in mind you will need to deal with restarting the download if it gets interrupting in the middle.)
Also be sure you correctly handle filesystem operations as described here: http://android-developers.blogspot.com/2010/12/saving-data-safely.html
An alternate idea if you're worried about missing data files... If at any point your app looks for a file and it doesn't exist, throw an exception, pass it to a handler that shows a dialog and 'verifies' your data. You can keep a list of all needed data files, and then only download ones that don't exist. Something like a system check, if you will.
That way they don't end up downloading 50MB if they were only missing a couple files they accidentally deleted in root explorer ;-)

Categories