您的位置:首页 > 理论基础 > 计算机网络

1、Android网络编程之获取网络图片资源

2013-03-27 12:23 423 查看
这只是一个简单的demo 具体的解释代码中重点的部分会给出注释 写的不足之处还请多多谅解

Android工程结构如下:



首先还是先贴上main.xml如下:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/hello" />

<EditText

android:id="@+id/edpath"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="http://192.168.1.100:8080/ServerForPicture_web/H1.png" >

<requestFocus />

</EditText>

<Button

android:id="@+id/btn_show"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Button" />

<ImageView

android:id="@+id/img_show"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/ic_launcher" />

</LinearLayout>

下面贴出主Activity代码;

package com.ServerForPicture.Activity;

import java.net.MalformedURLException;

import java.net.ProtocolException;

import android.app.Activity;

import android.graphics.Bitmap;

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;

public class ServerForPictureMainActivity extends Activity implements OnClickListener{

/** Called when the activity is first created. */

private Button btn_show;

private EditText edpath;

private ImageView img_show;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

findView();

btn_show.setOnClickListener(this);

}

private void showImage() throws MalformedURLException, ProtocolException {

// TODO Auto-generated method stub

String path = edpath.getText().toString();

Bitmap bitmap = ImageService.getImage(path);//下面会给出具体的类的声明和定义方法

img_show.setImageBitmap(bitmap);

}

private void findView() {

// TODO Auto-generated method stub

btn_show = (Button)findViewById(R.id.btn_show);

edpath = (EditText)findViewById(R.id.edpath);

img_show = (ImageView)findViewById(R.id.img_show);

}

public void onClick(View v) {

// TODO Auto-generated method stub

try {

showImage();

} catch (MalformedURLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (ProtocolException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

其中ImageService类如下:

package com.ServerForPicture.Activity;

import java.io.IOException;

import java.io.InputStream;

import java.net.HttpURLConnection;

import java.net.MalformedURLException;

import java.net.ProtocolException;

import java.net.URL;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

public class ImageService {

/**

* 1.url 获得文件路径

* 2.HttpURLConnection 打开链接

* 3.设置连接时间setConnectTomeout();

* 4.设置请求方式一般默认为“GET”

* @throws MalformedURLException

* @throws ProtocolException

*

*

* */

public static Bitmap getImage(String path) throws MalformedURLException, ProtocolException {

URL url = new URL(path);

HttpURLConnection conn;

try {

conn = (HttpURLConnection)url.openConnection();

conn.setConnectTimeout(5000);

conn.setRequestMethod("GET");

if(conn.getResponseCode() == 200){

InputStream inputStream = conn.getInputStream();

//将数据流使用BitmapFactory类的译码方法转换为Bitmap

Bitmap bitmap = BitmapFactory.decodeStream(inputStream);

return bitmap;

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return null;

}

}

ok至此客户端代码结束,服务器端工程目录如下:



上面只是建了一个Dynameic Web Project工程并在WebContent下面放了一个H1.png

服务器运行后,运行Android客户端结果如下

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: