1

I am programming a simple I/O system to store data needed by my Android application. I use an ObjectInputStream with the following initialization ("context" is the object obtained with getApplicationContext():

ObjectInputStream ois = new ObjectInputStream( 
 new BufferedInputStream(context.openFileInput("data.bin")));

Then I use ois.readObject() to fill an ArrayList.

My application works perfectly on the emulator, but crashes when calling the oos.close() method on a real device (Samsung Galaxy SII).

I cannot find where the problem is. Do I have to add some permissions or something like that to use internal storage? Have I forgotten something?

Thank you for your answers.

dda
  • 6,030
  • 2
  • 25
  • 34
  • 1
    You haven't shown enough code for anybody to tell you what's going on. Also how do you know it's the call to `close()` which causes the crash? The only way to tell would be to study the logcat which will tell you at which line the failure is occurring and it'll give a reason (exception stack trace, for example). – Squonk Mar 14 '12 at 00:40
  • Thank you for your answer.Giving all the code would have been useless, because I thought the problem was not in my code. Actually, the file I was trying to open on my real device did not exist, because it was the first time the application was launched. On the emulator, I created the file a "long time ago", and this file was still there, and usable. To fix this problem, I checked the files used by my application with context.fileList(). If the list doesn't contain my file, then I create it with an ObjectOutputStream object. However, I would like to know if there is a better solution. –  Mar 14 '12 at 00:50
  • There can be no 'better' solution - the file either exists or it doesn't and if it doesn't you need to create it, there's no two ways about it. BTW, you said in your original question (before you edited it) that you 'handle all exceptions'. That can't be the case as `context.openFileInput("data.bin")` would throw a `FileNotFoundException`. Ideally you should surround that line (in your question) with a try/catch block for that exception. If you hit the catch block then you know the file doesn't exist and you can create it.. – Squonk Mar 14 '12 at 01:05
  • Please define "crashes". There is probably an exception and stack trace with some really important information. – user207421 Mar 14 '12 at 02:54
  • Thank you for your answers. I will pay more attention to exception handling next time. –  Mar 14 '12 at 21:49

0 Answers0