0

我有我的应用程序下载图像的功能,但我不知道为什么我已经收到主线程异常的网络。 这是我的类功能:下载图像错误android.os.NetworkOnMainThreadException

公共类尾{

String story, description, title, location; 
int imageID; 
URL url; 

public URL getUrl() { 
    return url; 
} 

public void setUrl(URL url) { 
    this.url = url; 
} 

public Tail(int imageID, String location, String description, String title, String story) { 
    this.imageID = imageID; 
    this.location = location; 
    this.title = title; 
    this.description = description; 
    this.story = story; 
} 

public Tail(String url, String location, String description, String title, String story) { 
    try { 
     this.url = new URL(url); 
    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } 
    this.location = location; 
    this.title = title; 
    this.description = description; 
    this.story = story; 
} 

public String getStory() { 
    return story; 
} 

public void setStory(String story) { 
    this.story = story; 
} 


public String getLocation() { 
    return location; 
} 

public void setLocation(String location) { 
    this.location = location; 
} 

public int getImageID() { 
    return imageID; 
} 

public void setImageID(int imageID) { 
    this.imageID = imageID; 
} 

public String getDescription() { 
    return description; 
} 

public void setDescription(String description) { 
    this.description = description; 
} 

public String getTitle() { 
    return title; 
} 

public void setTitle(String title) { 
    this.title = title; 
} 

public static Bitmap getImageBitmapFromUrl(URL url) { 
    Bitmap bm = null; 
    try { 
     HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
     if (conn.getResponseCode() != 200) { 
      return bm; 
     } 
     conn.connect(); 
     InputStream is = conn.getInputStream(); 

     BufferedInputStream bis = new BufferedInputStream(is); 
     try { 
      bm = BitmapFactory.decodeStream(bis); 
     } catch (OutOfMemoryError ex) { 
      bm = null; 
     } 
     bis.close(); 
     is.close(); 
    } catch (Exception e) { 
    } 
    return bm; 
} 

知道还有什么方法可以解决这个问题?

回答