06-23-2011 12:03 PM
I'm really lost here. About one from 100 customers reports this error for one of my apps (different phones, different OS). The app creates a file on SDCard (or DeviceMemory if no SDCard), the code is not executed on startup, but after the users enters the app.
I've searched the forums - this errors is caused when file connections are not properly closed. However I think that I have done it right. Could you please look at that piece of code and let me know what I'm doing wrong?
Thanks a lot
try
{
Class classs = this.class;
InputStream readData = classs.getResourceAsStream("new" + getExtension(type));
byte[] docsData = IOUtilities.streamToBytes(readData);
readData.close();
String baseUrl;
if (hasMediaCard())
baseUrl = Constants.DOCS_URI_CARD;
else
baseUrl = Constants.DOCS_URI_STORE;
String fileURL = baseUrl + text + getExtension(type);
FileConnection fconn = (FileConnection)Connector.open(fileURL);
if (!fconn.exists())
{
fconn.create(); // create the file if it doesn't exist
}
DataOutputStream out = fconn.openDataOutputStream();
for (int i = 0; i < docsData.length; i++)
{
out.write(docsData[i]);
}
out.flush();
out.close();
fconn.close();
Status.show(fileURL + " created!", 2000);
errorMessField_.setText(fileURL + " created!");
}
catch (Exception ex)
{
Status.show("Error creating " + text, 2000);
errorMessField_.setText("Error: " + ex);
}
Solved! Go to Solution.
06-24-2011 01:09 PM - edited 06-24-2011 08:11 PM
I would code this:
FileConnection fconn = null;
InputStream readData = null;
DataOutputStream out = null;
try
{
Class classs = this.class;
readData = classs.getResourceAsStream("new" + getExtension(type));
byte[] docsData = IOUtilities.streamToBytes(readData);
readData.close();
readData = null;
String baseUrl;
if (hasMediaCard())
baseUrl = Constants.DOCS_URI_CARD;
else
baseUrl = Constants.DOCS_URI_STORE;
String fileURL = baseUrl + text + getExtension(type);
fconn = (FileConnection)Connector.open(fileURL);
if (!fconn.exists())
{
fconn.create(); // create the file if it doesn't exist
}
out = fconn.openDataOutputStream();
for (int i = 0; i < docsData.length; i++)
{
out.write(docsData[i]);
}
out.flush();
out.close();
out = null;
fconn.close();
fconn = null;
Status.show(fileURL + " created!", 2000);
errorMessField_.setText(fileURL + " created!");
}
catch (Exception ex)
{
Status.show("Error creating " + text, 2000);
errorMessField_.setText("Error: " + ex);
}
finally
{
try {
if ( readData != null ) {
readData.close();
}
} catch (Exception e) {
}
try {
if ( out != null ) {
out.close();
}
} catch (Exception e) {
}
try {
if ( fconn != null ) {
fconn.close();
}
} catch (Exception e) {
}
}
Note: Code not tested, or even comiled.
06-24-2011 01:44 PM
Always put the code that closes the Streams into a finally clause like peter shows in his code.
06-28-2011 09:32 AM
Thanks! That makes perfect sense, if some error occures in the try-catch block, connections would never close. Sigh, I should have figured it out by myself ![]()
One last question before I close the topic:
In this article stands that maximum number of file connections is 16 (OS4.7 and above). Is the maximum number for every app or for all apps in general?
If the number is for all apps - is it possible then that another app has already consumed all allowed file connections and when my app starts, it will always fail?
02-10-2013 05:26 AM
02-10-2013 05:44 AM
Please ask a new question on a new Thread.
And can you please clarify the question. What codes are you talking about? What problem are you trying to stop?
02-15-2013 06:30 AM
02-15-2013 06:46 AM
I am not clear what you are asking. The word codes can mean a number of things. Can you explain in more detail what you are asking?
In answer to the unanswered question from QuiteSimple, the limits are per Application and have been extended in later OS's, I think 16 actually applied to OS 4.5 and lower, the limit in newer OS is significantly higher. But this is from memory.
02-15-2013 07:12 AM
02-15-2013 07:57 AM
Assuming it works correctly, in my quick review, the code you have shown correctly opens and closes its connections. I can see a number of things that I am not happy about including:
1) There is no 'finally' clause that makes sure that the connections are in fact closed after an Exception
2) I'm not sure why you use a DataOutputStream in the following code:
DataOutputStream out = fconn.openDataOutputStream();
for (int i = 0; i < docsData.length; i++)
{
out.write(docsData[i]);
}
You could use just an OutputStream and write the whole buffer out at once.
3) Probably the most important.
You are doing this processing on the EventThread. I think you will get away with this if you have small files. For large files this will block an the OS might throw your app out. You should do operations like this on their own Thread. That makes advising the user problematic, but there are ways round that.
But in summary, from my quick review, if this is the only code you are running, then you will not see the error. So if you are seeing it, I suspect there is something else going on.