Android - 在显示进度时将图像从URL加载到ImageView(不保存图像)

时间:2016-09-22 20:39:48

标签: android android-asynctask android-imageview android-progressbar

请考虑以下AsyncTask。它旨在从URL下载图像并将其保存到设备,同时通过水平ProgressBar显示进度。现在我没有使用变量" bitmap"根本(它仍然是空的)。我想要做的事情有两件事:

  1. 将图像从URL加载到ImageView中而不将实际文件保存到我的设备中(这是否可能?),并在执行此操作时显示ProgressBar - 实际显示进度,并且不仅仅是一个动画圈。

  2. (对于任何了解毕加索的人)使用毕加索(http://square.github.io/picasso/)做同样的事情。 Picasso确实让一切变得简单,我可以使用这行代码:Picasso.with(getApplicationContext()).load(url).into(imageView); 然而,如果我能够在这样做时实际显示进度(再次使用实际显示进度的ProgressBar),那将是很好的。

  3. 任何帮助将不胜感激。另外,我很感激有关当前代码的任何反馈以及如何改进它(这主要是关注YouTube教程,所以我觉得我应该给予赞扬:https://www.youtube.com/watch?v=5HDr9FdGIVg)。

        private class ImageDownloadTask extends AsyncTask<String, Integer, Bitmap>{
        private int contentLength = -1;
        private URL downloadURL = null;
        private HttpURLConnection connection = null;
        private InputStream inputStream = null;
        private File file;
        private OutputStream outputStream = null;
        private int counter = 0;
        private Bitmap bitmap = null;
    
        @Override
        protected void onPreExecute()
        {
            setProgressBarProgress(0);
            showProgressBar();
        }
    
        @Override
        protected Bitmap doInBackground(String[] objects)
        {
            try
            {
                downloadURL = new URL(url);
                connection = (HttpURLConnection)downloadURL.openConnection();
                contentLength = connection.getContentLength();
                inputStream = connection.getInputStream();
                file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath() + "/" + Uri.parse(objects[0]).getLastPathSegment());
                outputStream = new FileOutputStream(file);
    
                byte buffer[] = new byte[1024];
                int read = -1;
    
                while ((read = inputStream.read(buffer)) != -1)
                {
                    outputStream.write(buffer, 0, read);
                    counter += read;
                    publishProgress(counter);
                };
            }
            catch (SecurityException e)
            {
                Msg.log("Security Exception: " + e.getMessage());
            }
            catch (Exception e)
            {
                Msg.log("Other Exception: " + e.getMessage());
            }
            finally
            {
                //Even if our attempt to download the image did not succeed,
                //we should still close the connection, and the streams.
                //Otherwise, we are potentially wasting the device's resources.
                if (connection!=null)
                    connection.disconnect();
    
                try {inputStream.close();}
                catch (IOException e) {e.printStackTrace();}
    
                try {outputStream.close();}
                catch (IOException e) {e.printStackTrace();}
    
                try {outputStream.flush();}
                catch (IOException e) {e.printStackTrace();}
            }
    
            return bitmap;
        }
    
        @Override
        protected void onProgressUpdate(Integer... values)
        {
            int progress = (int)(((double)values[0]/contentLength)*100);
            setProgressBarProgress(progress);
        }
    
        @Override
        protected void onPostExecute(Bitmap result)
        {
            hideProgressBar();
            if (result != null) {
                imageView.setImageBitmap(result);
            }
        }
    }
    

1 个答案:

答案 0 :(得分:0)

Picasso实际上无法为您提供下载进度,但您可以给它一个在图像加载完成时调用的回调。

最常用的方法是显示进度条/圆圈,并在下载完成后隐藏它。

已回答here