我需要在AsyncTask中下载图像并在progressDialog中显示进度,我在这里唯一能做的就是弄清楚如何使用1024字节步骤正确更新进度条,目前我有这个并且它没有工作
class DownloadImageTask extends AsyncTask<String, Integer, Void> {
@Override
protected Void doInBackground(String... url) {
bitmap = DownloadImage(url[0]);
return null;
}
@Override
protected void onProgressUpdate(Integer... args) {
mProgressDialog.setProgress(args[0]);
}
@Override
protected void onPostExecute(Void unused) {
ImageView img = (ImageView) findViewById(R.id.img);
img.setImageBitmap(bitmap);
img.setVisibility(View.INVISIBLE);
imgInfo = "Height: " + bitmap.getWidth() + " px" + "\n" + "Width: "
+ bitmap.getHeight() + " px" + "\n" + "File Size: "
+ fileSize / 1024 + " KB" + "\n" + "Image Name: "
+ getString(R.string.img_name);
isDownloaded = true;
mProgressDialog.dismiss();
}
@Override
protected void onPreExecute() {
mProgressDialog = new ProgressDialog(AndroidActivity.this);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setMessage("Loading...");
mProgressDialog.setCancelable(false);
mProgressDialog.show();
}
private Bitmap DownloadImage(String URL) {
InputStream in = null;
try {
in = OpenHttpConnection(URL);
bitmap = BitmapFactory.decodeStream(in);
in.close();
} catch (IOException e1) {
e1.printStackTrace();
}
return bitmap;
}
private InputStream OpenHttpConnection(String urlString)
throws IOException {
InputStream in = null;
int response = -1;
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
try {
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
fileSize = conn.getContentLength();
response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
// loop with step 1kb
byte data[] = new byte[1024];
long total = 0;
int count = 0;
while ((count = in.read(data)) != -1) {
total += count;
publishProgress((int)(total*100/fileSize));
}
}
} catch (Exception ex) {
throw new IOException("Error connecting");
}
return in;
}
}
有人可以帮忙吗?
答案 0 :(得分:1)
在doInBackground()中,您必须使用publishProgress()来显示更新。
在示例中查看,使用publishProgress();
http://developer.android.com/reference/android/os/AsyncTask.html
答案 1 :(得分:1)
您最好从doInBackground
返回位图,并从onPostExecute()
class DownloadImageTask extends AsyncTask<String, Integer, Bitmap> {
@Override
protected Bitmap doInBackground(String... url) {
Bitmap bitmap = DownloadImage(url[0]);
return bitmap ;
}
@Override
protected void onProgressUpdate(Integer... args) {
mProgressDialog.setProgress(args[0]);
}
@Override
protected void onPostExecute(Bitmap bmp) {
ImageView img = (ImageView) findViewById(R.id.img);
img.setImageBitmap(bmp);
img.setVisibility(View.INVISIBLE);
imgInfo = "Height: " + bitmap.getWidth() + " px" + "\n" + "Width: "
+ bitmap.getHeight() + " px" + "\n" + "File Size: "
+ fileSize / 1024 + " KB" + "\n" + "Image Name: "
+ getString(R.string.img_name);
isDownloaded = true;
mProgressDialog.dismiss();
}
**
**
一旦输入流到达终点,您就无法再从中读取数据。您应该将InputStream
写入byte array
,然后将bytearray转换为位图。
private Bitmap OpenHttpConnection(String urlString)
throws IOException {
........
........
byte data[] = new byte[1024];
long total = 0;
int count = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while ((count = in.read(data)) != -1) {
total += count;
bos.write(data, 0, count);
publishProgress((int)(total*100/fileSize));
}
return BitmapFactory.decodeByteArray(bos.toByteArray(),0,bos.size());
}