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;
    	}
    
  • 相关阅读:
    Luogu P2495 [SDOI2011]消耗战
    40. Combination Sum II
    39. Combination Sum
    22. Generate Parentheses
    51. N-Queens
    Codeforces Round #346 (Div. 2) E. New Reform
    Codeforces Round #346 (Div. 2) D. Bicycle Race
    HDU 5651xiaoxin juju needs help
    VK Cup 2016
    Educational Codeforces Round 10 D. Nested Segments
  • 原文地址:https://www.cnblogs.com/qq4004229/p/4350366.html
Copyright © 2011-2022 走看看