无法立即读取下载的文本文件



我有一个应用程序,可以将文本文件从web下载到应用程序专用文件夹/data/data/com.example.app。下载该文件时,我需要读取一些数据。

我的应用程序中的代码:

private class DownloadTextFile extends AsyncTask<String, Integer, String> {
    @Override
    protected String doInBackground(String... sUrl) {
        // download text file 
    }
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // read text file
        }

当我尝试读取文本文件时,我得到了一个file not found错误
当我关闭应用程序并重新打开它时,应用程序会很好地读取下载的文本文件。

编辑:

哈哈,我太笨了。谢谢大家how can i make this question as answered

您应该读取异步类的onPostExecute(...)方法中的数据
由于onPostExecute(...)方法将在doInBackground(...)方法完成处理后执行,因此在调用doInbackground(...)方法之前调用onPreExecution(...)方法。因此,当前您正在尝试打开尚未在doInBackground(...)方法中下载的文件
您的代码应该是这样的:

private class DownloadTextFile extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... sUrl) {
    // download text file 
}
@Override
protected void onPostExecute(.....) {
    super.onPostExecute();
    // read text file
    }

最新更新