2015-10-28 1 views
0

Привет в ниже загружаемых картинах для показа progessbar до 100, но полностью 100% изображений не отображаются, пока загружаются и не отображаются. I want after 100% Я хочу перейти к активности.Загрузка изображений с процентом progessbar

Но это занимает время, чтобы переместить следующую деятельность.

ява

public class DownloadTask extends AsyncTask<Void, Void, String> { 


     protected void onPreExecute() { 
      super.onPreExecute(); 
      final DialogProgressBarRunnable progressDialog = 
        new DialogProgressBarRunnable(getActivity(), false, 2); 
      progressDialog.show(); 
      // the dialog box shouldn't get cancelled when clicking outside it or pressing back button. 
      progressDialog.setCanceledOnTouchOutside(false); 
      progressDialog.setCancelable(false); 

      // pd.setMessage("Downloading catalogue images."); 
      // pd.show(); 

     } 

     protected String doInBackground(Void... Params) { 


      parsingObject = new ParsingForFinalImages(catid, responseJson); 
      /* ConnectionDetector cd = new ConnectionDetector(getActivity().getBaseContext()); 
      Boolean isInternetPresent = cd.isConnectingToInternet(); 

      if (isInternetPresent==true) 
      { 

      } 
      */ 

      // put your code here 
      // JSON parsing begins here via the parsing class 
      // Put this code in async task 


      return "Success"; 

     } 

     protected void onPostExecute(String result) { 
      super.onPostExecute(result); 
      // pd.hide(); 
      // pd.dismiss(); 
      Intent intent = new Intent(getActivity(), ImageGallery.class); 
      startActivity(intent); 
     } 
    } 
    private class DialogProgressBarRunnable extends ProgressDialog implements 
      Runnable { 
     private boolean showSecondary; 
     private int incrementAfter; 

     public DialogProgressBarRunnable(Context context, 
             boolean showSecondary, int incrementAfter) { 
      super(context); 
      setCancelable(true); 
      setMessage(getString(R.string.download_message)); 
      setSecondaryProgress(0); 
      setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
      setMax(100); 
      setProgress(0); 
      this.showSecondary = showSecondary; 
      this.incrementAfter = incrementAfter; 
     } 

     @Override 
     public void show() { 
      super.show(); 
      new Thread(this).start(); 
     } 

     @Override 
     public void run() { 
      while (progress < 100) { 
       progress++; 
       try { 
        Thread.sleep(1000); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 
       // increment the first/second progress bar after every % 
       progressBar(); 
      } 
     } 

     private void progressBar() { 
      if (progress % incrementAfter == 0) { 

       progressFirstBar(); 
      } 
      if (showSecondary) { 
       progressSecondaryBar(); 
      } 
     } 

     private void progressSecondaryBar() { 
      while (secondaryProgress < 100) { 
       secondaryProgress++; 
       try { 
        Thread.sleep(50000); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 
       handler.post(new Runnable() { 

        @Override 
        public void run() { 

         setSecondaryProgress(secondaryProgress); 
        } 
       }); 
      } 
     } 

     private void progressFirstBar() { 
      secondaryProgress = 0; 
      handler.post(new Runnable() { 

       @Override 
       public void run() { 
        setProgress(progress); 

        if (progress == 100) { 

         dismiss(); 

        } 

       } 
      }); 

     } 
    } 

ответ

0

класс DownloadFileFromURL расширяет AsyncTask {

/** 
    * Before starting background thread Show Progress Bar Dialog 
    * */ 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     showDialog(progress_bar_type); 
    } 


    /** 
    * After completing background task Dismiss the progress dialog 
    * **/ 

    @SuppressWarnings("deprecation") 
    @Override 
    protected String doInBackground(Void... params) { 

     // TODO Auto-generated method stub 
     // Declear Variables 

     int count; 
     try { 
      URL url1 = new URL(url); 
      URLConnection conection = url1.openConnection(); 
      conection.connect(); 

      // this will be useful so that you can show a tipical 0-100% 
      // progress bar 
      int lenghtOfFile = conection.getContentLength(); 

      // download the file 
      InputStream input = new BufferedInputStream(url1.openStream(), 
        8192); 

      // Output stream 
      OutputStream output = new FileOutputStream(Environment 
        .getExternalStorageDirectory().toString() + "/Report.xls"); 

      byte data[] = new byte[1024]; 

      long total = 0; 

      while ((count = input.read(data)) != -1) { 
       total += count; 
       // publishing the progress.... 
       // After this onProgressUpdate will be called 
       publishProgress("" + (int) ((total * 100)/lenghtOfFile)); 

       // writing data to file 
       output.write(data, 0, count); 
       Log.d("Downloding"+data,"Count"+count); 
      } 

      // flushing output 
      output.flush(); 

      // closing streams 
      output.close(); 
      input.close(); 

     } catch (Exception e) { 
      Log.e("Error: ", e.getMessage()); 
     } 

     return null; 
    } 

    /** 
    * Updating progress bar 
    * */ 
    @Override 
    protected void onProgressUpdate(String... progress) { 
     // setting progress percentage 
     pDialog.setProgress(Integer.parseInt(progress[0])); 
    } 



    /** 
    * After completing background task Dismiss the progress dialog 
    * **/ 
    @SuppressWarnings("deprecation") 
    @Override 
    protected void onPostExecute(String reString) { 
     // dismiss the dialog after the file was downloaded 
     super.onPostExecute(null);; 
     dismissDialog(progress_bar_type); 
     Log.d("Download","Completed"); 
     Intent intent1=new Intent(DownloadExcle.this,MainActivity.class); 
     intent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     startActivity(intent1); 
    } 
} 
+0

в моем коде, где я сделал ошибку – care567

 Смежные вопросы

  • Нет связанных вопросов^_^