ENOENT (No such file or directory) when trying to open a file downloaded from URL with AsyncTask

杀马特。学长 韩版系。学妹 提交于 2019-12-14 02:23:44

问题


I download a file from URL with AsyncTask. The file is saved and I can see it on my sdcard. In my app I want to open this file after it is downloaded but there is following error:

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

Do I have to wait a specified time between downloading and opening the file? What is the problem?

I have these both permissions:

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

My 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]);
     }
 }

This codes tries to open the saved file:

/**
 * 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 ;

}


回答1:


I changed the following line and it works:

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


来源:https://stackoverflow.com/questions/13051407/enoent-no-such-file-or-directory-when-trying-to-open-a-file-downloaded-from-ur

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!