Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 显示进度条直到下载完成-Android_Java_Android - Fatal编程技术网

Java 显示进度条直到下载完成-Android

Java 显示进度条直到下载完成-Android,java,android,Java,Android,我正在使用以下代码从网站下载视频。下载功能运行良好,进度条有一个小问题。 进度条保持不变,不会随着值的增加而更新 @Override protected Dialog onCreateDialog(int id) { switch (id) { case progress_bar_type: // we set this to 0 pDialog = new ProgressDialog(this); pDialog.set

我正在使用以下代码从网站下载视频。下载功能运行良好,进度条有一个小问题。 进度条保持不变,不会随着值的增加而更新

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
        case progress_bar_type: // we set this to 0
            pDialog = new ProgressDialog(this);
            pDialog.setMessage("Downloading file. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setMax(100);
            pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            pDialog.setCancelable(true);
            pDialog.show();
            return pDialog;
        default:
            return null;
    }
}

class DownloadFile1 extends AsyncTask<String, Integer, String> {
    ProgressDialog bar;
    public String videoToDownload;
    public String fileName;

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

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

        try {
            mp4load(videoToDownload);
        } catch (Exception e) {
            // TODO: handle exception
        }

        return null;
    }

    public void mp4load(String urling) {
        try {
            System.out.println("Downloading");
            URL url = new URL(urling);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod("GET");
            //c.setDoOutput(true);
            con.connect();

            // String downloadsPath = Environment.getExternalStoragePublicDirectory();
            File SDCardRoot = Environment.getExternalStorageDirectory();

            File outputFile = new File(SDCardRoot, fileName);

            if (!outputFile.exists()) {
                outputFile.createNewFile();
            }

            FileOutputStream fos = new FileOutputStream(outputFile);

            int status = con.getResponseCode();

            InputStream is = con.getInputStream();

            byte[] buffer = new byte[1024];
            int len1 = 0;
            while ((len1 = is.read(buffer)) != -1) {
                fos.write(buffer, 0, len1);
            }
            fos.close();
            is.close();
            System.out.println("Downloaded");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

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

    /**
     * After completing background task
     * Dismiss the progress dialog
     **/
    @Override
    protected void onPostExecute(String file_url) {
        // dismiss the dialog after the file was downloaded
        dismissDialog(progress_bar_type);
    }
}
@覆盖
受保护的对话框onCreateDialog(int id){
开关(id){
案例进度条类型://我们将其设置为0
pDialog=新建进度对话框(此对话框);
setMessage(“正在下载文件,请稍候…”);
pDialog.setUndeterminate(假);
pDialog.setMax(100);
pDialog.setProgressStyle(ProgressDialog.STYLE_水平);
pDialog.setCancelable(真);
pDialog.show();
返回pDialog;
违约:
返回null;
}
}
类DownloadFile1扩展了异步任务{
进程对话框栏;
公共字符串videoToDownload;
公共字符串文件名;
/**
*在启动后台线程之前
*显示进度条对话框
*/
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
显示对话框(进度条类型);
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
整数计数;
试一试{
mp4load(视频下载);
}捕获(例外e){
//TODO:处理异常
}
返回null;
}
公共void mp4load(字符串URL化){
试一试{
System.out.println(“下载”);
URL=新URL(URL链接);
HttpURLConnection con=(HttpURLConnection)url.openConnection();
con.setRequestMethod(“GET”);
//c、 设置输出(真);
con.connect();
//字符串downloadsPath=Environment.getExternalStoragePublicDirectory();
文件SDCardRoot=Environment.getExternalStorageDirectory();
File outputFile=新文件(SDCardRoot,文件名);
如果(!outputFile.exists()){
outputFile.createNewFile();
}
FileOutputStream fos=新的FileOutputStream(outputFile);
int status=con.getResponseCode();
InputStream=con.getInputStream();
字节[]缓冲区=新字节[1024];
int len1=0;
而((len1=is.read(buffer))!=-1){
fos.写入(缓冲区,0,len1);
}
fos.close();
is.close();
System.out.println(“下载”);
}捕获(IOE异常){
e、 printStackTrace();
}
}
/**
*更新进度条
*/
受保护的void onProgressUpdate(字符串…进度){
//设置进度百分比
pDialog.setProgress(Integer.parseInt(progress[0]));
}
/**
*完成后台任务后
*关闭进度对话框
**/
@凌驾
受保护的void onPostExecute(字符串文件\u url){
//下载文件后关闭对话框
解雇对话框(进度条类型);
}
}
进度条始终保持0%的进度,并在下载完成后消失。
如何更改进度条的更新部分?

在doInBackground中调用
publishProgress
方法,传递要在进度条中设置的整数

protected String doInBackground(String... params) {
    int count;

    try {
        mp4load(videoToDownload);
    } catch (Exception e) {
        // TODO: handle exception
    }

    return null;
}
您需要将代码从方法mp4load移动到doInBackground,以便像这样修改代码:

System.out.println("Downloading");
            URL url = new URL(urling);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod("GET");
            //c.setDoOutput(true);
            con.connect();

            // String downloadsPath = Environment.getExternalStoragePublicDirectory();
            File SDCardRoot = Environment.getExternalStorageDirectory();

            File outputFile = new File(SDCardRoot, fileName);

            if (!outputFile.exists()) {
                outputFile.createNewFile();
            }

            FileOutputStream fos = new FileOutputStream(outputFile);

            int status = con.getResponseCode();

            InputStream is = con.getInputStream();
            int fileLength = con.getContentLength();
            long total = 0;
            byte[] buffer = new byte[1024];
            int len1 = 0;
            while ((len1 = is.read(buffer)) != -1) {
                fos.write(buffer, 0, len1);
                total += len1;
                publishProgress((int) (total * 100 / fileLength));
            }
            fos.close();
            is.close();

想法很简单,您需要获得文件的大小,并在下载文件时缓慢地用更新进度条。

在您的doInBackground中调用
publishProgress
方法传递要在进度条中设置的整数

protected String doInBackground(String... params) {
    int count;

    try {
        mp4load(videoToDownload);
    } catch (Exception e) {
        // TODO: handle exception
    }

    return null;
}
您需要将代码从方法mp4load移动到doInBackground,以便像这样修改代码:

System.out.println("Downloading");
            URL url = new URL(urling);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod("GET");
            //c.setDoOutput(true);
            con.connect();

            // String downloadsPath = Environment.getExternalStoragePublicDirectory();
            File SDCardRoot = Environment.getExternalStorageDirectory();

            File outputFile = new File(SDCardRoot, fileName);

            if (!outputFile.exists()) {
                outputFile.createNewFile();
            }

            FileOutputStream fos = new FileOutputStream(outputFile);

            int status = con.getResponseCode();

            InputStream is = con.getInputStream();
            int fileLength = con.getContentLength();
            long total = 0;
            byte[] buffer = new byte[1024];
            int len1 = 0;
            while ((len1 = is.read(buffer)) != -1) {
                fos.write(buffer, 0, len1);
                total += len1;
                publishProgress((int) (total * 100 / fileLength));
            }
            fos.close();
            is.close();
想法很简单,您需要获得文件的大小,并在下载文件时使用缓慢更新progressbar。

参考,您的progress type参数必须是整数,因为您的AsyncTask类声明如下

class DownloadFile1 extends AsyncTask<String, Integer, String> {
public void onProgressUpdate(Integer..Progress)

然后使用覆盖注释引用,您的进度类型参数必须是整数,因为您的AsyncTask类声明如下

class DownloadFile1 extends AsyncTask<String, Integer, String> {
public void onProgressUpdate(Integer..Progress)

然后使用覆盖注释

pDialog.setProgress((int)(total*100/fileLength));这个用于mepDialog.setProgress((int)(总计*100/文件长度));这个对我有用