Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android 獲取網絡圖片

android 獲取網絡圖片

編輯:關於Android編程

主函數

package com.example.internetone;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity implements OnClickListener {

	Button btn;
	ImageView imageView;
	String path;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
				.detectDiskReads().detectDiskWrites().detectNetwork()
				.penaltyLog().build());
		StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
				.detectLeakedSqlLiteObjects().detectLeakedClosableObjects()
				.penaltyLog().penaltyDeath().build());

		findViews();

	}

	private void findViews() {
		btn = (Button) this.findViewById(R.id.button1);
		imageView = (ImageView) this.findViewById(R.id.imageView1);
		btn.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		path = "http://cimg2.163.com/catchpic/4/44/44ED4853B4A3FE71971C885A0A22825E.jpg";		
		functionOne(path);
	}
	

	/**
	 * 獲取網絡圖片的方法
	 * */
	private void functionOne(String path) {
		byte[] data;
		try {
			data = NetTool.getImage(path);
			Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length);
			imageView.setImageBitmap(bm);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

輔助類,用於獲取圖片信息,連接網絡

package com.example.internetone;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class NetTool {

    /**
     * 獲取指定路徑,的數據。
     *
     * **/
    public static byte[] getImage(String urlpath) throws Exception {
        URL url = new URL(urlpath);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setConnectTimeout(6 * 1000);
        // 別超過10秒。
        System.out.println("------conn.getResponseCode()--------" + conn.getResponseCode());
        if (conn.getResponseCode() == 200) {
            InputStream inputStream = conn.getInputStream();
            return readStream(inputStream);
        }
        return null;
    }

    /**
     * 讀取數據 輸入流
     *
     * */
    public static byte[] readStream(InputStream inStream) throws Exception {
        ByteArrayOutputStream outstream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = -1;
        while ((len = inStream.read(buffer)) != -1) {
            outstream.write(buffer, 0, len);
        }
        outstream.close();
        inStream.close();

        return outstream.toByteArray();
    }
}
布局文件



    
AndroidManifest.xml配置網絡權限
uses-permission android:name="android.permission.INTERNET"/>
個人整理的,用於參考


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved