URL rul = new URL(imageUrl);
HttpURLConnection connection =  (HttpURLConnection) rul.getContent();
connection.setRequestMethod("GET");
connection.setReadTimeout(3000);
return Drawable.createFromStream(connection.getInputStream(),
"image.png");
我去网上获得一个图片,在getgetInputStream()之前怎样判断这个图片是不是存在的呢?

解决方案 »

  1.   

    get的时候好像有个返回值,从这个返回值判断的哦。
      

  2.   

    connection.getInputStream()通过获取流,检查数据包的头的属性及大小
      

  3.   

    connection.getResponseCode可以判断 正常返回200表示存在
      

  4.   

    给你段代码,我是写入文件try {
                                HttpGet request = new HttpGet(url);
                                HttpClient httpClient = new DefaultHttpClient();                            HttpResponse response = httpClient.execute(request);
                                if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                                    FileOutputStream fos = new FileOutputStream(f);
                                    response.getEntity().writeTo(fos);
                                    fos.flush();
                                    fos.close();
                                } else {
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }