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客户端结果如下
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客户端结果如下
相关文章推荐
- Android 异步获取网络图片Bitmap资源
- android 网络编程--URL获取数据/图片
- android 网络编程:显示从网络获取的图片
- android 网络编程--URL获取数据/图片
- Android编程实现获取图片资源的四种方法
- Android进阶2之Https访问网络资源(获取网络图片)
- Android进阶2之Https访问网络资源(获取网络图片)
- android网络编程之——客户端获取网页的图片
- Android网络编程--获取网络图片
- 【Android网络编程】获取网络图片,具有缓存功能
- Android编程之从网络上获取图片查看器和HTML代码的查看器
- android HttpURLConnection编程详解,获取网络图片,html代码查
- android HttpURLConnection编程详解,获取网络图片,html代码查看
- Android开发之异步获取并下载网络资源-下载图片和下载文本内容
- Android编程简单获取网络上的图片
- android_学习—获取网络数据(图片,html)
- android获取网络图片的用法 BitmapFactory.decodeByteArray 返回null
- Android编程获取网络连接状态(3G/Wifi)及调用网络配置界面
- android获取网络图片的用法 BitmapFactory.decodeByteArray
- android获取网络图片