Go Back   Android Forums > Android Development > Application Development
Application Development Dev lounge for our application developers.

Get excited for the Samsung Galaxy S5! Find everything you need and discuss it in our Galaxy S5 Forum!

test: Reply
 
LinkBack Thread Tools
Old December 30th, 2012, 04:23 PM   #1 (permalink)
New Member
Thread Author (OP)
 
Zpecter's Avatar
 
Join Date: Sep 2012
Location: Málaga (Spain)
Gender: Male
Posts: 4
 
Device(s): Apple iPhone 4S Samsung Galaxy S I9000 Samsumng Omnia 7 I8700
Carrier: Yoigo

Thanks: 0
Thanked 0 Times in 0 Posts
Default Crashes while writing a file into internal storage

Hello, I am developing an application and I have a problem when I try to save a file into the internal storage.

The file is an xml and it's on the res/raw folder on my project. When the application starts, it checks if the file exists or not. If it doesn't exist, it copies the file from the res/raw folder into the internal storage, else it retrieves a field inside the file to check its version and if it's a newer version, it deletes the previous file and copies it again.

Well, the problem is that if I have a new version and I have deleted the previous one, when I'm going to copy it again, it throws an NullPointerExceptcion and I can't find the problem, but I'm using the same code for both situations and I don't know how to solve it.

This is the code I'm using:

[HIGH]private void writeFileToInternalStorage() {
InputStream fXmlFile = getResources().openRawResource(R.raw.xmlFile);
FileOutputStream fos = null;
try {
File dir = getFilesDir();
File file = new File(dir, "file.xml");
if(file.exists()) {
String versionRecursos = getFileVersion(fXmlFile);
String versionLocal = getFileVersion(new FileInputStream(file));
if (!versionRecursos.equalsIgnoreCase(versionLocal)) {
file.delete();
fos = openFileOutput("file.xml", Context.MODE_PRIVATE);
copyFile(fos, fXmlFile);
}
}
else {
fos = openFileOutput("file.xml", Context.MODE_PRIVATE);
copyFile(fos, fXmlFile);
}
}
catch(IOException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

protected void copyFile(OutputStream fos, InputStream fXmlFile) throws IOException {
byte[] buffer = new byte[1024];
int bytesRead = 0;
while((bytesRead = fXmlFile.read(buffer)) > 0){
fos.write(buffer, 0, bytesRead);
}
}[/HIGH]Any tips for solving it??

PS: The exception is thrown on this statement:

[HIGH]while((bytesRead = fXmlFile.read(buffer)) > 0)[/HIGH]

Zpecter is offline  
Reply With Quote
sponsored links
Reply


Go Back   Android Forums > Android Development > Application Development
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 05:08 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.