2013-03-03 50 views
1

我正在開發應用程序,並且在嘗試將文件保存到內部存儲器時出現問題。將文件寫入內部存儲器時崩潰

該文件是一個XML文件,它在我的項目上的res/raw文件夾中。當應用程序啓動時,它會檢查文件是否存在。如果它不存在,它將文件從res/raw文件夾複製到內部存儲器中,否則它將檢索文件內的字段以檢查其版本,如果它是較新的版本,則刪除以前的文件並再次複製它。

嗯,問題是,如果我有一個新版本,我刪除了前一個,當我要複製它時,它會拋出一個NullPointerExceptcion,我找不到問題,米使用相同的代碼爲這兩種情況,我不知道如何解決它。

這是我使用的代碼:

private void writeFileToInternalStorage() { 
     FileOutputStream fos = null; 
    try { 
     InputStream fXmlFile = getResources().openRawResource(R.raw.partidos); 
     File file = getBaseContext().getFileStreamPath("file.xml"); 
     if(file.exists()) { 
      String resourcesVersion= getFileVersion(fXmlFile); 
      String localVersion = getFileVersion(new FileInputStream(file)); 
      if (!resourcesVersion.equalsIgnoreCase(localVersion)) { 
       //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); 
     } 
    } 

    protected String getFileVersion(InputStream file) { 
     String version = null; 
     try { 
      DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); 
      DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); 
      Document doc = dBuilder.parse(file); 
      doc.getDocumentElement().normalize(); 
      version = doc.getElementsByTagName("numero").item(0).getFirstChild().getNodeValue(); 
     } 
     catch (ParserConfigurationException e) { 
      e.printStackTrace(); 
     } 
     catch (SAXException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return version; 
    } 

解決它的任何提示?

PS:異常被拋出這句話:

while((bytesRead = fXmlFile.read(buffer)) > 0) 

PS2:這是logcat的代碼:

03-03 20:47:07.140: W/System.err(2166): java.lang.NullPointerException: asset 
03-03 20:47:07.148: W/System.err(2166):  at android.content.res.AssetManager.readAsset(Native Method) 
03-03 20:47:07.158: W/System.err(2166):  at android.content.res.AssetManager.access$700(AssetManager.java:35) 
03-03 20:47:07.168: W/System.err(2166):  at android.content.res.AssetManager$AssetInputStream.read(AssetManager.java:573) 
03-03 20:47:07.168: W/System.err(2166):  at com.jfma75.myapp.MyApp.copyFile(MyApp.java:291) 
03-03 20:47:07.178: W/System.err(2166):  at com.jfma75.myapp.MyApp.writeFileToInternalStorage(MyApp.java:263) 
03-03 20:47:07.178: W/System.err(2166):  at com.jfma75.myapp.MyApp.onCreate(MyApp.java:62) 
03-03 20:47:07.188: W/System.err(2166):  at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:999) 
03-03 20:47:07.188: W/System.err(2166):  at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4151) 
03-03 20:47:07.198: W/System.err(2166):  at android.app.ActivityThread.access$1300(ActivityThread.java:130) 
03-03 20:47:07.198: W/System.err(2166):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1255) 
03-03 20:47:07.208: W/System.err(2166):  at android.os.Handler.dispatchMessage(Handler.java:99) 
03-03 20:47:07.208: W/System.err(2166):  at android.os.Looper.loop(Looper.java:137) 
03-03 20:47:07.218: W/System.err(2166):  at android.app.ActivityThread.main(ActivityThread.java:4745) 
03-03 20:47:07.218: W/System.err(2166):  at java.lang.reflect.Method.invokeNative(Native Method) 
03-03 20:47:07.228: W/System.err(2166):  at java.lang.reflect.Method.invoke(Method.java:511) 
03-03 20:47:07.237: W/System.err(2166):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
03-03 20:47:07.237: W/System.err(2166):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
03-03 20:47:07.248: W/System.err(2166):  at dalvik.system.NativeStart.main(Native Method) 
+0

嘗試刪除'file.delete()',它只是簡單地覆蓋在問題的文件 – Matt 2013-03-03 19:30:12

+0

粘貼logcat的。 – 2013-03-03 19:34:09

+0

@Matt,我試過了,但我仍然有同樣的問題。 – Specter 2013-03-03 19:51:33

回答

1

剛剛添加的回答我的意見,因爲它似乎解決OP的問題,將關閉這個問題:

我想我知道是什麼問題。你打開資源文件,然後 將它保存在InputStream中,然後你可以撥打String resourcesVersion= getFileVersion(fXmlFile);,我想這是你自己的私有功能 來獲得版本。我打賭你在該功能中提前InputStream或關閉 。然後,當您稍後致電copyFile時, InputStream已消耗或已關閉。嘗試只刪除該行 並把恆定String resourcesVersion = "versionThatDoesn'tMatch"; 只是爲了測試InputStream

0

看來,在聲明中

InputStream fXmlFile = getResources().openRawResource(R.raw.partidos); 

變量fXmlFilenull。你可以檢查爲什麼發生。