【问题标题】:Android Help with adding a progress dialog while image loading?Android帮助在图像加载时添加进度对话框?
【发布时间】:2011-08-24 23:58:51
【问题描述】:

我一直在关注将图像远程下载到图像视图的教程,但我不确定如何添加进度对话框(图像或其他内容)以向用户显示图像正在下载,而不仅仅是空白屏幕.

希望有人能帮忙

 ImageView imView;
 String imageUrl="http://domain.com/images/";
 Random r= new Random();
/** Called when the activity is first created. */ 
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN ,
            WindowManager.LayoutParams.FLAG_FULLSCREEN );

    setContentView(R.layout.galleryshow);

    Button bt3= (Button)findViewById(R.id.get_imagebt);
    bt3.setOnClickListener(getImgListener);
    imView = (ImageView)findViewById(R.id.imview);
}    

View.OnClickListener getImgListener = new View.OnClickListener()
{

      @Override
      public void onClick(View view) {
           // TODO Auto-generated method stub


           int i =r.nextInt(114);
           downloadFile(imageUrl+"image-"+i+".jpg");
           Log.i("im url",imageUrl+"image-"+i+".jpg");
      }

};


Bitmap bmImg;
void downloadFile(String fileUrl){
      URL myFileUrl =null;          
      try {
           myFileUrl= new URL(fileUrl);
      } catch (MalformedURLException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
      }
      try {
           HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
           conn.setDoInput(true);
           conn.connect();
           int length = conn.getContentLength();
           InputStream is = conn.getInputStream();

           bmImg = BitmapFactory.decodeStream(is);
           imView.setImageBitmap(bmImg);
      } catch (IOException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
      }
 }
}

【问题讨论】:

标签: android dialog imageview progress


【解决方案1】:

你应该看看这个:asynctask 是要走的路。

Android : Loading an image from the Web with Asynctask

问候, 斯蒂芬

【讨论】:

  • 感谢 Stephane,但我是一名 android 新手,您提供的链接仅供我管理。我认为如果我可以将这个进度对话框应用到我已经在使用的代码中,这将对我有所帮助,所以至少我可以了解一切是如何工作的。您是否可以向我展示如何在我提供的代码中实现它。谢谢你,露西
【解决方案2】:

您需要查看ProgressBar 类,并且基本上在图像加载时使用它。或者,您可以在下载图像时放置默认图像。

要将进度条放在你的代码中,最简单的基本上就是翻转它们在布局中的可见性。

  1. 在您的布局中,您有两件事。一个占位符用于ProgressBar,另一个用于图片
  2. 进度条最初设置为 VISIBLE,图像设置为 GONE
  3. 执行 AsyncTask(见下文)后,您需要翻转可见性。基本上把progressBar改为GONE,把图片改为VISIBLE

这是您应该尝试做的事情。检查代码中的 NOTE 和 TODO 注释。注意:我只是修改了你的代码,但没有运行它,但这应该足以说明这个想法。

一些关键点:

  1. 可能会阻塞 UI 线程的长时间运行的任务应该在 AsyncTask 中执行。在您的情况下,这将是下载图像
  2. 需要在 UI Thread 中处理的 Post 执行应该在 postExecute() 中处理
  3. 在捕获异常期间执行 e.printStacktrace() 不是一个好习惯。如果没有适当的句柄,则无法正确处理此异常,并且将来可能会导致错误。此外,在生产过程中,当客户端出现错误时,此信息对您完全没有帮助,因为它只是在控制台中打印出来


    View.OnClickListener getImgListener = new View.OnClickListener()
    {
        @Override
        public void onClick(View view) {
            // NOTE: here you need to show the progress bar, you could utilize ProgressBar class from Android
            // TODO: Show progress bar

            AsyncTask asyncTask = new AsyncTask() {
                @Override
                public Bitmap doInBackground(Void... params) {
                    int i =r.nextInt(114);

                    // NOTE: move image download to async task
                    return downloadFile(imageUrl+"image-"+i+".jpg");
                }

                @Override
                public void onPostExecute(Bitmap result) {
                    // TODO: hide the progress bar here 
                    // and flip the image to VISIBLE as noted above
                    if(result != null) {
                        imView.setImageBitmap(result);
                    } else {
                        // NOTE 3: handle image null here, maybe by showing default image
                    }
                }
            };

            // NOTE: execute in the background so you don't block the thread
            asyncTask.execute();
        }
    };

    // Change the return type to Bitmap so we could use it in AsyncTask
    Bitmap downloadFile(String fileUrl){
        URL myFileUrl =null;          
        try {
            myFileUrl= new URL(fileUrl);
        } catch (MalformedURLException e) {
            // NOTE: You should not have e.printStacktrace() here. In fact
            // printStacktrace is a bad practice as it doesn't really do anything
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
            conn.setDoInput(true);
            conn.connect();
            int length = conn.getContentLength();
            InputStream is = conn.getInputStream();

            Bitmap bmImg = BitmapFactory.decodeStream(is);

            // return image this to the main Thread
            return bmImg;
        } catch (IOException e) {
            return null;
        }
    }

【讨论】:

  • 感谢陌陌的深入解答。非常赞赏。此刻,这一切听起来都有些过头了。 (新手在这里摇晃)我不完全确定从哪里开始。我将查看您的笔记和建议,并尝试一一实施。您在笔记中提到要从 android 获取进度条,我在哪里可以找到它?对不起,如果我听起来很无知,当我不努力学习安卓的时候,我真的很好!再次感谢..
  • Luci,这是 ProgressBar developer.android.com/reference/android/widget/ProgressBar.html 的链接,您可以将其放入布局中并在图像下载时显示
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多