中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

Android 通過(guò)網(wǎng)絡(luò)獲取圖片的代碼

2018-07-20    來(lái)源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬(wàn)Linux鏡像隨意使用

Android 通過(guò)網(wǎng)絡(luò)獲取圖片的代碼

主activity
package com.netimg;


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

public class AndroidnetimgActivity extends Activity {
    /** Called when the activity is first created. */
	//定義所使用的組件
	private Button button;
	private EditText editText;
	private ImageView imagesView;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //實(shí)例化用到的組件
        editText =(EditText)findViewById(R.id.EditText);
        imagesView = (ImageView)findViewById(R.id.ImageView);
        button = (Button)findViewById(R.id.Button);
        
        //為按鈕添加監(jiān)聽(tīng)事件
        button.setOnClickListener(new buttonListener());
        
    }
    
    
    private final class buttonListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			String path = editText.getText().toString();
			
			System.out.println(path);
			
			//通過(guò)業(yè)務(wù)類ImageService的getImage方法得到數(shù)據(jù)
			try {
				byte[] data = ImageService.getImage(path);
				Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
				imagesView.setImageBitmap(bitmap);//顯示圖片
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				Toast.makeText(getApplicationContext(), "獲取失敗", 1).show();
			}
			
			
		}
    	
    }
}
 
業(yè)務(wù)類
package com.netimg;

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





public class ImageService {

	public static byte[] getImage(String path) throws Exception {
	
		URL url = new URL(path);
		//基于HTTP協(xié)議連接對(duì)象
		HttpURLConnection conn = (HttpURLConnection)url.openConnection();
		
		conn.setConnectTimeout(5000);
		conn.setRequestMethod("GET");
		
		if(conn.getResponseCode() == 200){
			InputStream inStream = conn.getInputStream();
			return StreamTool.read(inStream);
		}
		
		return null;
	}

}
 
工具類
package com.netimg;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class StreamTool {

	public static byte[] read(InputStream inStream) throws IOException {
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int len = 0;
		while( (len = inStream.read(buffer)) != -1){
			outStream.write(buffer, 0, len);
		}
		inStream.close();
		return outStream.toByteArray();
	}

} 
獲取網(wǎng)絡(luò)權(quán)限
<uses-permission android:name="android.permission.INTERNET"/>

標(biāo)簽: 代碼 權(quán)限 網(wǎng)絡(luò)

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:Android獲取系統(tǒng)正在運(yùn)行的后臺(tái)服務(wù)列表

下一篇:Java實(shí)現(xiàn)的utf8,gbk,unicode編碼相互轉(zhuǎn)換的代碼