Download a file from url and save

37 Views Asked by At

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

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

    private Context mContext;
    private ProgressDialog mProgressDialog;

    public DownloadFileTask(Context context) {
        mContext = context;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mProgressDialog = new ProgressDialog(mContext);
        mProgressDialog.setTitle("Please wait.");
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        mProgressDialog.setCancelable(true);
        mProgressDialog.setMax(100);
        mProgressDialog.show();
    }

    @Override
    protected String doInBackground(String... params) {
        String fileUrl = params[0];
        String fileName = params[1];
        try {
            URL url = new URL(fileUrl);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.connect();

            int fileSize = urlConnection.getContentLength();
            InputStream inputStream = urlConnection.getInputStream();
            FileOutputStream outputStream = new FileOutputStream(new File(mContext.getFilesDir(), fileName));

            byte[] buffer = new byte[1024];
            int bytesRead;
            long totalBytesRead = 0;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                totalBytesRead += bytesRead;
                publishProgress((int) ((totalBytesRead * 100) / fileSize));
                outputStream.write(buffer, 0, bytesRead);
            }
            outputStream.close();
            inputStream.close();
            urlConnection.disconnect();
            return "Download successful";
        } catch (Exception e) {
            Log.i("TAG", "doInBackground: " + e.getMessage());
            return e.toString();
        }
    }

    @Override
    protected void onProgressUpdate(Integer... progress) {
        super.onProgressUpdate(progress);
        if (progress[0] == 0) {
            mProgressDialog.setTitle("Please wait.");
        } else {
            mProgressDialog.setTitle("Downloading in progress.");
        }
        mProgressDialog.setProgress(progress[0]);
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        Toast.makeText(mContext, result, Toast.LENGTH_SHORT).show();
        mProgressDialog.dismiss();
    }
}

Hello friends, I want to download a specific file for each item when clicked on in the RecyclerView. Currently, it displays a progress bar, and when it completes, it returns this line to me:

return "Download successful";

But no file is being saved in the program's memory. I would appreciate it if you could help me. Thank you in advance for your assistance.

0

There are 0 best solutions below