gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 11:10:46 27 4
gpt4 key购买 nike

我使用以下代码从互联网下载视频:

class DownloadFile1 extends AsyncTask<String, Integer, String> {

public String videoToDownload;
public String fileName;

@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();
}
}

我想添加进度条,直到视频下载完毕。进度条应该以任何格式从下载开始到下载结束显示(即,它可以是圆形进度条等) .如何做到这一点?

我有一个在异步任务中添加进度条的代码。以下代码是否正确?

                        @Override
protected void onPostExecute(Void result) {
bar.dismiss();
super.onPostExecute(result);
}
@Override
protected void onPreExecute() {
bar = new ProgressDialog(activity);
bar.setMessage("Processing...");
bar.setIndeterminate(true);
super.onPreExecute();
}

最佳答案

更改您的代码,如下所示:

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

public DownloadFile1(Context context)
{
mContext=context;
}
@Override
protected void onPreExecute()
{
super.onPreExecute();
bar = new ProgressDialog(mContext);
bar.setMessage("Processing...");
bar.setIndeterminate(true);
}

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

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

return null;
}

@Override
protected String onPostExecute()
{
super.onPostExecute(result);
bar.dismiss();
}

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();
}
}

并更改调用方式,例如

DownloadFile1 downloadFile1 = new DownloadFile1(mContext); 

或者

DownloadFile1 downloadFile1 = new DownloadFile1(MainActivity.this); 

然后

downloadFile1.videoToDownload = video_url;
downloadFile1.fileName = video_url;
downloadFile1.execute();

关于java - 显示进度条直到下载完成 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50643305/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com