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;
    	}
    
  • 相关阅读:
    fullCalendar改造计划之带农历节气节假日的万年历(转)
    Linked List Cycle
    Remove Nth Node From End of List
    Binary Tree Inorder Traversal
    Unique Binary Search Trees
    Binary Tree Level Order Traversal
    Binary Tree Level Order Traversal II
    Plus One
    Remove Duplicates from Sorted List
    Merge Two Sorted Lists
  • 原文地址:https://www.cnblogs.com/qq4004229/p/4350366.html
Copyright © 2011-2022 走看看