• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

Android 如何使用网络获取图片的代码

Android 水墨上仙 1511次浏览

Android 通过网络获取图片的代码

主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);
        //实例化用到的组件
        editText =(EditText)findViewById(R.id.EditText);
        imagesView = (ImageView)findViewById(R.id.ImageView);
        button = (Button)findViewById(R.id.Button);
        
        //为按钮添加监听事件
        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);
			
			//通过业务类ImageService的getImage方法得到数据
			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();
			}
			
			
		}
    	
    }
}

业务类

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协议连接对象
		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();
	}
}

获取网络权限

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


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明Android 如何使用网络获取图片的代码
喜欢 (0)
加载中……