1

Am use this code progress bar show but percentage bar not running

I am fairly new to android development, so what I am trying to make is app that can show pdf from url,

Am use this code progress bar show but percentage bar not running

I am using com.github.barteksc.pdfviewer.PDFView to show pdf

here is my pdf show activity

public class test2 extends AppCompatActivity  {

PDFView pdfView; //pdfView object
String URL;
String fileName;
File directory; //path of created File

// Container for all parameters of DownloadAsync
private static class AsyncParameters {
    String URL;
    File directory;
    AsyncParameters(String URL, File directory) {
        this.URL = URL;
        this.directory = directory;
    }
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent = getIntent(); //whatever calls this activity, gather the intent
    URL = intent.getStringExtra("File URL"); // in this case, get the file name of the "extra" passed through
    fileName = intent.getStringExtra("File Name");

    setContentView(R.layout.activity_test2);

    File intDirectory = getFilesDir();
    File folder = new File(intDirectory, "pdf");
    boolean isDirectoryCreated = folder.exists();
    //setDownloadButtonListener();
    if (!isDirectoryCreated) {
        isDirectoryCreated= folder.mkdir();
    }
    if(isDirectoryCreated) {
        directory = new File(folder, fileName);
        try {
            directory.createNewFile();
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        //See if file already exists (reduces wait time)
        boolean empty = directory.length() == 0;
        if (empty) {
            /**Call class to create parameter container **/
            AsyncParameters param = new AsyncParameters(URL, directory);
            DownloadAsync Downloader = new DownloadAsync();
            Downloader.execute(param);
        }
        showPdf();
    }


}
public void showPdf()
{
    pdfView = (PDFView) findViewById(R.id.pdfViewPager);
    pdfView.fromFile(directory).load();
}

public class DownloadAsync extends AsyncTask<AsyncParameters, Void, Void> {

    // Container for all parameters of DownloadAsync
    ProgressDialog pDialog;

    @Override
    protected void onPreExecute() {
        //Create a progress bar that details the program downloading
        super.onPreExecute();
        pDialog = new ProgressDialog(test2.this);
        pDialog.setMessage("Downloading ");
        String message= "please wait don't push back";

        SpannableString ss2 =  new SpannableString(message);
        ss2.setSpan(new RelativeSizeSpan(1f), 0, ss2.length(), 0);
        ss2.setSpan(new ForegroundColorSpan(Color.BLACK), 0, ss2.length(), 0);

        pDialog.setMessage(ss2);
        pDialog.setCancelable(false);
        pDialog.setIndeterminate(false);
        pDialog.setMax(100);
        pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        pDialog.show();
    }

    @Override
    protected Void doInBackground(AsyncParameters... params) {
        int count;
        String fileURL = params[0].URL;
        File directory = params[0].directory;
        try {
            FileOutputStream f = new FileOutputStream(directory);
            java.net.URL u = new URL(fileURL);
            HttpURLConnection c = (HttpURLConnection) u.openConnection();
            c.connect();
            InputStream in = c.getInputStream();
            int length=c.getContentLength();

            byte[] data;
            data = new byte[1024];

            long total = 0;

            while ((count = in.read(data)) != -1) {
                total += count;
                f.write(data, 0, count);
            }

            f.flush();
            in.close();
            in.close();

        } catch (Exception e) {
            e.printStackTrace();
            pDialog.setMessage(new SpannableString("ERROR DOWNLOADING"));
        }
        onPostExecute();
        return null;
    }

    private void onPostExecute() {
        pDialog.dismiss();
        showPdf();
    }
}
}

   
Ichigo Kurosaki
  • 3,765
  • 8
  • 41
  • 56
Ram
  • 11
  • 2

1 Answers1

0

I don't see that you are calling publishProgress in doing background to invoke OnProgressUpdate.I don't see that you are setting the percentage in onProgressUpdate.I don't see that you have oveerriden onProgressUpdate anywhere. OnPostExecute() is automatically called when background execution finishes.You don't need to call explicitly in doInBackGround and should not call it explicitly.

Note: AsyncTask is deprecated for API level 30.

Rishabh Ritweek
  • 550
  • 3
  • 10