pathText = "http://192.168.1.100:8080/ServerForPicture/wangjialin.jpg"
1 import java.io.InputStream; 2 import java.net.HttpURLConnection; 3 import java.net.URL; 4 5 import android.graphics.Bitmap; 6 import android.graphics.BitmapFactory; 7 8 public class ImageService { 9 10 /** 11 * 获取图片 12 * @param path 图片路径 13 * @return 14 */ 15 public static Bitmap getImage(String path) throws Exception{ 16 17 URL url = new URL(path); 18 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 19 conn.setConnectTimeout(5000); 20 conn.setRequestMethod("GET"); 21 22 if(conn.getResponseCode() == 200){ 23 InputStream inStream = conn.getInputStream(); 24 Bitmap bitmap = BitmapFactory.decodeStream(inStream); 25 return bitmap; 26 /*byte[] data = StreamTool.read(inStream); 27 Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); 28 return bitmap;*/ 29 } 30 return null; 31 } 32 33 }
显示
1 public void showimage(View v){ 2 String path = pathText.getText().toString(); 3 try { 4 Bitmap bitmap = ImageService.getImage(path); 5 imageView.setImageBitmap(bitmap); 6 } catch (Exception e) { 7 e.printStackTrace(); 8 Toast.makeText(getApplicationContext(), R.string.error, 1).show(); 9 } 10 }