2011-01-12 121 views
0
public Object fetch(String address) throws MalformedURLException, 
IOException { 
    URL url = new URL(address); 
    Object content = url.getContent(); 
    return content; 
} 

private Drawable ImageOperations(Context ctx, String url) { 
    try { 
     InputStream is = (InputStream) this.fetch(url); 
     Drawable d = Drawable.createFromStream(is, "src"); 
     return d; 
    } catch (MalformedURLException e) { 
     return null; 
    } catch (IOException e) { 
     return null; 
    } 
    catch (Exception e) 
    { 
     return null; 
    } 
} 

try { 
      Drawable a =ImageOperations(this,"url"); imgView.setImageDrawable(a); 
     } catch (Exception e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 

这工作,但在罕见ocasions应用程序冻结由于“SocketException:ADRESS家人不支持协议”。有没有什么办法解决这一问题?由于问题加载图像

回答

0

您正在尝试下载从UI线程文件...(这就是为什么你的UI冻结)

使用单独的线程或AsyncTask使您的用户界面不会冻结。

这应该可以解决您的问题。

+0

好吧,我试着用这个线程的第一个答案:http://stackoverflow.com/questions/3090650/android-loading-an-image-from-the-web-with-asynctask,但“DownloadImageTask” - 新的DownloadImageTask.execute(mChart);“在eclipse中获得红色,即使创建了该类。我错过了什么? – Johan 2011-01-12 12:45:10

0

正如st0le指出的那样,您正试图从UI线程中完成重型任务。

Android中的所有重型应用程序都应该在其他worker thread上完成。因为在main thread(或UI线程)中执行此操作可能会使您的应用程序无响应,并且可能会在系统被认为已挂起时被杀死。

所以你必须在单独的线程中做长时间运行的操作。为了实现这个,你可以使用Handlers的概念。