2012-10-24 31 views
1

我使用AsyncTask从URL下载文件。该文件已保存,我可以在我的SD卡上看到它。在我的应用我想打开此文件在下载后,但有以下错误:当使用AsyncTask尝试打开从URL下载的文件时,ENOENT(没有这样的文件或目录)

java.io.FileNotFoundException: /mnt/sdcard/XML/zurt.xml: open failed: ENOENT (No such file or directory) 

我必须等待下载和打开文件之间的指定时间?问题是什么?

我有这两种权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" > 
</uses-permission> 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" > 
</uses-permission> 

我的AsyncTask:

/** 
    * Download XML from URL async 
    */ 
    // usually, subclasses of AsyncTask are declared inside the activity class. 
    // that way, you can easily modify the UI thread from here 
    private class DownloadFile extends AsyncTask<String, Integer, String> { 
    @Override 
    protected String doInBackground(String... sUrl) { 
     try { 
      URL url = new URL(sUrl[0]); 
      URLConnection connection = url.openConnection(); 
      connection.connect(); 
      // this will be useful so that you can show a typical 0-100% progress bar 
      int fileLength = connection.getContentLength(); 

      // create a File object for the parent directory 
      File xmlDirectory = new File(Environment.getExternalStorageDirectory()+ FileSeperator+"XML" 
        +FileSeperator); 
      // have the object build the directory structure, if needed. 
      xmlDirectory.mkdirs(); 

      // create a File object for the output file 
      File outputFile = new File(xmlDirectory, Name+FileExtension); 
      // download the file 
      InputStream input = new BufferedInputStream(url.openStream()); 
      OutputStream output = new FileOutputStream(outputFile); 

      byte data[] = new byte[1024]; 
      long total = 0; 
      int count; 
      while ((count = input.read(data)) != -1) { 
       total += count; 
       // publishing the progress.... 
       publishProgress((int) (total * 100/fileLength)); 
       output.write(data, 0, count); 
      } 

      output.flush(); 
      output.close(); 
      input.close(); 
     } catch (Exception e) { 
      System.out.println(e); 
     } 
     return null; 
    } 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     mProgressDialog.show(); 
    } 

    protected void onPostExecute() { 
     mProgressDialog.dismiss(); 
    } 

    @Override 
    protected void onProgressUpdate(Integer... progress) { 
     super.onProgressUpdate(progress); 
     mProgressDialog.setProgress(progress[0]); 
    } 
} 

该代码试图打开保存文件:

/** 
* XML Parser 
* */ 
private ArrayList<Datapoint> parseXML() { 

try {   
    Log.w("AndroidParseXMLActivity", "Start"); 
    /** Handling XML */ 
    SAXParserFactory spf = SAXParserFactory.newInstance(); 
    SAXParser sp = spf.newSAXParser(); 
    XMLReader xr = sp.getXMLReader(); 

    File file = new File(Environment.getExternalStorageDirectory()+ FileSeperator+"XML" 
    +FileSeperator+ Name + FileExtension); 

    XMLContentHandler myXMLHandler = new XMLContentHandler(); 
    xr.setContentHandler(myXMLHandler); 

     xr.parse(new InputSource(new InputStreamReader(new FileInputStream(file)))); 

    itemsList = myXMLHandler.getItemsList(); 

    Log.w("AndroidParseXMLActivity", "Done"); 
} 
catch (Exception e) { 
    Log.w("AndroidParseXMLActivity",e); 
} 
return itemsList ; 

}

+0

显示下载后从中访问文件的代码。 –

+1

创建文件后,添加此打印语句,以确保您正在写入的文件是预期的文件:'Log.i(“FILE”,outputFile.getName());' – Phil

+0

@Raghav我已将代码添加到我的主要职位。 –

回答

3

我改变下面的lin e和它的工作原理:

protected void onPostExecute() { 
     parseXML(); 
     mProgressDialog.dismiss(); 
    } 
相关问题