0

I have created an App in which I have used the Fragment class and another Async Class for download two different class now I have to show the progress bar horizontal updated the progress bar how much downloading happen in onProgressUpdate. But onProgressUpdate the interface in which callback is not working and it is not updating the ProgressBar.

FragmentA.class

private void downloadAndUnzipContent(String url){
    progressBar.setVisibility(View.VISIBLE);
    progressBar.setProgress(0);
    DownloadFileAsync download = new DownloadFileAsync(path+"content.zip", mContext,
            new DownloadFileAsync.PostDownload(){
        @Override
        public void downloadDone(File file) {
            Log.i(TAG, "file download completed");
        }
    }, new DownloadFileAsync.ProgressUpdate() {

        @Override
        public void ProgressUpdate(int progress) {
            progressBar.setProgress(progress);
        }

    });
    download.execute(url);
}

In DownloadFileAsync.class where I am downloading the file:

public class DownloadFileAsync extends AsyncTask<String, Integer, String> {

private static final String TAG = "DOWNLOADFILE";

public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
private PostDownload callback;
private ProgressUpdate callProgress;
private Context context;
private FileDescriptor fd;
private File file;
private String downloadLocation;

public DownloadFileAsync(String downloadLocation, Context context, PostDownload callback, ProgressUpdate callProgress) {
    this.context = context;
    this.callback = callback;
    this.callProgress = callProgress;
    this.downloadLocation = downloadLocation;
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
}

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

    try {
        URL url = new URL(aurl[0]);
        URLConnection connection = url.openConnection();
        connection.connect();

        int lenghtOfFile = connection.getContentLength();
        Log.d(TAG, "Length of the file: " + lenghtOfFile);

        InputStream input = new BufferedInputStream(url.openStream());
        file = new File(downloadLocation);
        FileOutputStream output = new FileOutputStream(file); //context.openFileOutput("content.zip", Context.MODE_PRIVATE);
        Log.d(TAG, "file saved at " + file.getAbsolutePath());
        fd = output.getFD();

        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.flush();
        output.close();
        input.close();
    } catch (Exception e) {
    }
    return null;

}

protected void onProgressUpdate(Integer... progress) {
    //Log.d(TAG,progress[0]);
    if(callProgress != null)
        callProgress.ProgressUpdate(progress[0]);
}

@Override
protected void onPostExecute(String unused) {
    //progressBar.setVisibility(View.GONE);
    if (callback != null) callback.downloadDone(file);
}

public  interface PostDownload {
    void downloadDone(File fd);
}

public interface ProgressUpdate {
    void ProgressUpdate(int progress);
}
}

Have I made a mistake in the code?

halfer
  • 19,824
  • 17
  • 99
  • 186
Atul Dhanuka
  • 1,453
  • 5
  • 20
  • 56
  • https://stackoverflow.com/questions/3028306/download-a-file-with-android-and-showing-the-progress-in-a-progressdialog – Vij Jun 23 '17 at 07:30
  • There is no need of interface and it can be directly done from async task class – Avinash Roy Jun 23 '17 at 07:38
  • 2
    u just need to hold the references of the Ui elements of the fragment via an activity and u can directly update the progress bar from onProgressUpdate of the async class since u have the context variable – Avinash Roy Jun 23 '17 at 07:42

1 Answers1

0

your code is no problem. please debug check

 int lenghtOfFile = connection.getContentLength();

the "lenghtOfFile" is right. not all servers are returned to the ContentLength. sometimes it is -1.