2011-08-24 64 views
0

我一直在关注一个远程下载图像到图像视图的教程,但我不知道如何添加进度对话框(图像或其他)来显示用户图像正在下载,而不是一个空白的屏幕。Android帮助在图像加载时添加进度对话框?

希望有人能帮助

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

准确重复http://stackoverflow.com/questions/2735102/ideal-way-to-cancel-an-executing- asynctask/2740204#2740204 – Dharmendra

回答

0

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

要将进度条放入您的代码中,最简单的方法就是在布局中翻转其可见性。

  1. 在你的布局中,你有两件事。对于ProgressBar一个占位符,另一种是对进度设置为初始可见的图像
  2. 和图像GONE
  3. 你执行的AsyncTask(见下文)后,你需要翻转的知名度。基本上将progressBar更改为GONE并将图像更改为可见

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

一些要点:

  1. 长期运行的任务可能会阻止UI线程应的AsyncTask得到执行。在你的情况下,这将是下载图像
  2. 需要在UI中处理的执行后置线程应该在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; 
     } 
    } 

+0

感谢莫莫为您提供的深入解答。非常赞赏。这一切听起来有点夸张。 (新手摇这里)我不完全确定从哪里开始。我会查看你的笔记和建议,并尝试逐一实施。你在笔记中提到要从android获取进度条,我在哪里找到它?对不起,如果我听起来无知,我真的很好,当我不拔头发试图学习机器人!再次感谢.. – Lucy

+0

Luci,这里是ProgressBar http://developer.android.com/reference/android/widget/ProgressBar.html的链接,您可以在布局中放置并在图像下载时显示 – momo

1

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

Android : Loading an image from the Web with Asynctask

问候, 斯特凡

+0

感谢Stephane,但我是一个android新手,你提供的链接只是为了我的技术管理。我认为这会帮助我,如果我可以将这个进度对话框应用到我已经使用的代码上,那么至少我可以让我的头脑知道everthing是如何工作的。是否有可能向我展示如何在我提供的代码中实现它。谢谢,露西 – Lucy