zoukankan      html  css  js  c++  java
  • http获取图片信息

    一、安卓访问网络需要AndroidManifest.xml配置这样一个节点

    <manifest>

     <uses-permission android:name="android.permission.INTERNET" />
    </manifest>

    二、获取图片两种方法

    第一种:

    public Bitmap loadImageFromUrl(String url) throws Exception {
    		final DefaultHttpClient client = new DefaultHttpClient();
    		final HttpGet getRequest = new HttpGet(url);
    
    		HttpResponse response = client.execute(getRequest);
    		int statusCode = response.getStatusLine().getStatusCode();
    		if (statusCode != HttpStatus.SC_OK) {
    			Log.e("PicShow", "Request URL failed, error code =" + statusCode);
    		}
    
    		HttpEntity entity = response.getEntity();
    		if (entity == null) {
    			Log.e("PicShow", "HttpEntity is null");
    		}
    		InputStream is = null;
    		ByteArrayOutputStream baos = new ByteArrayOutputStream();
    		try {
    			is = entity.getContent();
    			byte[] buf = new byte[1024];
    			int readBytes = -1;
    			while ((readBytes = is.read(buf)) != -1) {
    				baos.write(buf, 0, readBytes);
    			}
    		} finally {
    			if (baos != null) {
    				baos.close();
    			}
    			if (is != null) {
    				is.close();
    			}
    		}
    		byte[] imageArray = baos.toByteArray();
    		return BitmapFactory.decodeByteArray(imageArray, 0, imageArray.length);
    	}
    

    第二种方法:

    	public byte[] getImage(String path) throws Exception {
    		URL url = new URL(path);
    		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    		conn.setConnectTimeout(5 * 1000);
    		conn.setRequestMethod("GET");
    		InputStream inStream = conn.getInputStream();
    		if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
    			return readStream(inStream);
    		}
    		return null;
    	}
    
  • 相关阅读:
    工资低的.Net程序员,活该你工资低
    React- jsx的使用 使用 camelCase 语法来设置内联样式. React 会在指定元素数字后自动添加 px
    React 使用jsx
    node.js 简介
    转换成数值 parseInt与parseFloat; (toString) 转化为字符串
    函数 封装性划分私有空间
    favicon 不显示的问题总结1
    js进阶 offset
    前端缓存技术
    图片的预加载与懒加载
  • 原文地址:https://www.cnblogs.com/qq4004229/p/4350366.html
Copyright © 2011-2022 走看看