2016-03-08 53 views
0

当我从我的浏览器打开php文件:“http://192.168.1.30/save/load_image_from_db.php?id=”+ ID; 图像显示正常,但是当我尝试从android应用程序显示它不起作用,我得到这个错误:skimageDecoder :: Factory返回null。Android的skimageDecoder ::工厂返回null

Bitmap对象仍然为空,即使当我将它分配给它从url.opencnnection()。getStream()获得的流的decodeStream!

代码:

@Override 
      protected Bitmap doInBackground(String... params) { 
       String id = params[0]; 
       String add = "http://192.168.1.30/save/load_image_from_db.php?id="+id; 
       URL url; 
       Bitmap image = null; 
       try { 
        url = new URL(add); 
        InputStream is = url.openConnection().getInputStream(); 
        image = BitmapFactory.decodeStream(is); 
       } catch (MalformedURLException e) { 
        e.printStackTrace(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
       return image; 
      } 
     } 

回答

0

试试这个:

@Override 
protected Bitmap doInBackground(String... params) { 
     String id = params[0]; 
     String add = "http://192.168.1.30/save/load_image_from_db.php?id="+id;   
     Bitmap image = null; 

     HttpClient client = new DefaultHttpClient(); 
     HttpGet request = new HttpGet(add); 
     HttpResponse response; 
     try { 
      response = (HttpResponse)client.execute(request);   
      HttpEntity entity = response.getEntity(); 
      BufferedHttpEntity bufferedEntity = new BufferedHttpEntity(entity); 
      InputStream inputStream = bufferedEntity.getContent(); 
      image = BitmapFactory.decodeStream(inputStream); 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return image; 
} 

希望它能帮助。

+0

我使用api 23,我没有得到class HttpClient,我不赞成使用api 23 –

+0

您可以在应用程序的gradle文件中添加useLibrary'org.apache.http.legacy'来使用HttpClient –

+0

为什么建议还原回[不推荐使用和效率较低的实现](https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-apache-http-client)?必须有一个解决方案使用当前的'HttpUrlConnection'(或可能是另一个开源库,如OkHttp)。 – Bryan