当前位置: 代码迷 >> Android >> android http远程获取图片解决方法
  详细解决方案

android http远程获取图片解决方法

热度:27   发布时间:2016-05-01 21:31:58.0
android http远程获取图片
String http = "http://192.168.1.154:8081/onLineSearchReport/images/upload/tspecial/bg_02.gif";
URL url = new URL(http);
URLConnection conn = url.openConnection();
conn.connect();
InputStream in = conn.getInputStream();
Bitmap map = BitmapFactory.decodeStream(in);
image01.setBackgroundDrawable(new BitmapDrawable(map));

在运行的时候,执行到InputStream in = conn.getInputStream();行代码的时候抛出异常。执行不过去。
在IE浏览器当中输入地址能够获取到图片。

如果使用HttpClient.....方法的时候,
HttpPost方法调用也会出错,哪位大侠帮帮忙啊??

------解决方案--------------------
楼主试试下面的代码
Java code
                URL image_url = new URL("http://192.168.1.154:8081/onLineSearchReport/images/upload/tspecial/bg_02.gif");URLConnection image_conn = image_url.openConnection();image_conn.connect();                    int image_file_size = image_conn.getContentLength();if(image_file_size <= 0)    return;                    // local_path + File.separator + image_file_name是你本地保存图片文件的名字,比如/sdcard/bg_02.gifFile image_file = new File(local_path + File.separator + image_file_name);InputStream image_is = image_conn.getInputStream();FileOutputStream image_fos = new FileOutputStream(image_file);byte image_buffer[] = new byte[1024];int image_number_read_bytes;while((image_number_read_bytes = image_is.read(image_buffer)) != -1){    image_fos.write(image_buffer, 0, image_number_read_bytes);}image_fos.flush();image_is.close();image_fos.close();
  相关解决方案