Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在加载图像时添加进度对话框的Android帮助?_Android_Dialog_Imageview_Progress - Fatal编程技术网

在加载图像时添加进度对话框的Android帮助?

在加载图像时添加进度对话框的Android帮助?,android,dialog,imageview,progress,Android,Dialog,Imageview,Progress,我一直在遵循一个教程,远程下载图像到imageview,但我不知道如何添加一个进度对话框(图像或其他东西)来向用户显示正在下载的图像,而不仅仅是一个空白屏幕 希望有人能帮忙 ImageView imView; String imageUrl="http://domain.com/images/"; Random r= new Random(); /** Called when the activity is first created. */ @Override public void

我一直在遵循一个教程,远程下载图像到imageview,但我不知道如何添加一个进度对话框(图像或其他东西)来向用户显示正在下载的图像,而不仅仅是一个空白屏幕

希望有人能帮忙

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

您应该看到这一点:asynctask是一个不错的选择

问候,, Stéphane

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

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

  • 在布局中,有两件事。一个占位符用于图像,另一个占位符用于图像
  • progressbar设置为“初始可见”,图像设置为“消失”
  • 执行AsyncTask(请参见下文)后,需要翻转可见性。基本上将progressBar更改为GONE,将图像更改为VISIBLE
  • 以下是你应该尝试做的。检查代码中的注释和TODO注释。注意:我刚刚修改了您的代码,但还没有运行它,但这应该足以说明这个想法

    一些要点:

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

  • 完全重复谢谢Stephane,但我是一个android新手,您提供的链接只是技术链接,我可以管理。我想,如果我能将这个进度对话框应用到我已经在使用的代码中,这会对我有所帮助,这样至少我可以了解每件事情是如何工作的。您是否可以告诉我如何在我提供的代码中实现它。谢谢你,LucyThanks Momo,谢谢你的深入回答。非常感谢。现在一切听起来都有点过头了。(新手在这里发抖)我不太确定从哪里开始。我将仔细阅读您的笔记和建议,并尝试逐一实施。你在笔记中提到要从android获取进度条,我在哪里可以找到?对不起,如果我听起来很无知,我真的很好,当我不是在努力学习android的时候!再次感谢..Luci,这是到ProgressBar的链接,您可以在下载图像时将其放入布局并显示出来
    
    
        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;
            }
        }