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

这只是一个简单的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客户端结果如下

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值