每次打开应用程序时重新下载文件

时间:2014-09-25 20:46:14

标签: android download zipfile

我的应用程序取决于我从URL下载并解压缩的PDF文件,工作正常 但是每次打开文件都会重新下载file.zip,即使文件已下载并存在 文件太大,所以它很麻烦,很难使用。

我希望你能帮助你 非常感谢

public class MainActivity extends Activity {


 String Url="www....zip";
 String unzipLocation = Environment.getExternalStorageDirectory() + "/unzipFolder/";
 String StorezipFileLocation =Environment.getExternalStorageDirectory() + "/DownloadedZip"; 
 String DirectoryName=Environment.getExternalStorageDirectory() + "/unzipFolder/files/";

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    DownloadZipfile mew = new DownloadZipfile();
    mew.execute(Url);



class DownloadZipfile extends AsyncTask<String, String, String>
{
     String result ="";
     @Override
     protected void onPreExecute()
     {

      super.onPreExecute();
      mProgressDialog = new ProgressDialog(MainActivity.this);
      mProgressDialog.setMessage("Downloading ... ");
      mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
      mProgressDialog.setCancelable(false);
      mProgressDialog.show();
     }

     @Override
     protected String doInBackground(String... aurl)
     {
      int count;

      try
      {
           URL url = new URL(aurl[0]);
           URLConnection conexion = url.openConnection();
           conexion.connect();
           int lenghtOfFile = conexion.getContentLength();
           InputStream input = new BufferedInputStream(url.openStream());

           OutputStream output = new FileOutputStream(StorezipFileLocation);

           byte data[] = new byte[1024];
           long total = 0;

           while ((count = input.read(data)) != -1)
               {
                total += count;
                publishProgress(""+(int)((total*100)/lenghtOfFile));
                output.write(data, 0, count);
               }
           output.close();
           input.close();
           result = "true";
      } 

      catch (Exception e) {

       result = "false";  }
      return null;

     }

     protected void onProgressUpdate(String... progress)
     {
      Log.d("ANDRO_ASYNC",progress[0]);
      mProgressDialog.setProgress(Integer.parseInt(progress[0]));
     }

     @Override
     protected void onPostExecute(String unused)
     {
      mProgressDialog.dismiss();
      if(result.equalsIgnoreCase("true"))
      {
       try
       {
        unzip();
       } catch (IOException e)
       {
        // TODO Auto-generated catch block
        e.printStackTrace();
       }
      }
      else
      {

      }
     }
    }

3 个答案:

答案 0 :(得分:0)

检查文件是否存在

 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

try
{

    File file = new File(StorezipFileLocation);
    if ( ! file.exists())
    {
        DownloadZipfile mew = new DownloadZipfile();
        mew.execute(Url);
    }

}
catch (FileNotFoundException fnfe1)
{
} 

答案 1 :(得分:0)

嗯,每次创建活动时都会调用onCreate(),每次打开它时都会发生。无论应用程序是否应下载,您都必须设置变量或保存在SharedPreference对象中。例如,您可以添加一个字段boolean isDownloaded = false,并在AsyncTask中完成下载后将其设置为true

或者,您可以检查要保存的文件是否已存在,这意味着您之前已将其保存在那里(if((new File(path)).exists())) {...})。但是,如果下载导致文件损坏,如果您不处理此问题,则将始终保留此损坏文件,否则因为这样做并不能保证下载已成功完成。

我提到的另一个选项是通过调用SharedPreferences获取默认值PreferenceManager.getDefaultSharedPreferences(this)并使用它来设置并获取一个布尔字段,告诉您是否下载了pdf。

第二种方法的优点是应用程序将记住即使在重新启动后或从内存中完全卸载应用程序后文件是否已下载。

答案 2 :(得分:0)

解决方法是检查文件是否存在..文件(路径)

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

File f = new File(Environment.getExternalStorageDirectory()+"/unzipFolder/files/");
 if(f.exists() == false)
  { 
     DownloadZipfile mew = new DownloadZipfile();
     mew.execute(Url);
  }
}
感谢所有帮助或尝试过的人...