开发者

ProgressDialog upade while image is downloading

I need to download a image in a AsyncTask and display progress in a progressDialog, the only thing i cant manage to do here is figure out how to update the progressbar properly with a 1024 bytes step, currently i have this and it doesn't work

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);
        mProgressDialo开发者_运维问答g.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;
    }
}

can someone help ?


You better return the bitmap from doInBackground and take it from parameter in 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();
}

**

EDIT :

**
Once inputstream reaches its end, you cannot read anymore data from it. You should write the InputStream to a byte array instead and then convert the bytearray to a bitmap.

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


In doInBackground() , you have to use publishProgress() to show update. Check here in the example, usage of publishProgress();
http://developer.android.com/reference/android/os/AsyncTask.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜