您好,欢迎来到乌哈旅游。
搜索
您的当前位置:首页android上把服务端的图片传到客户端并且在客户端显示到模拟器上

android上把服务端的图片传到客户端并且在客户端显示到模拟器上

来源:乌哈旅游
111==首先就是到MyEclipse服务端新建web project service 服务项目==

进到webroot中新建img文件夹==然后复制进去k1.jpg与k2.jpg图片2张===== 再rumas-MyEclipse ServerApplication==发布项目到tomcat6.0== 此时就可以在浏览器中访问图片了=======

服务端就这些东西=========

222==客户端新建一个android项目a07072client=============

到注册文件中添加联网权限intnet======= 布局文件中就一空的imageview==

xmlns:android=\"http://schemas.android.com/apk/res/android\" xmlns:tools=\"http://schemas.android.com/tools\" android:layout_width=\"match_parent\" android:layout_height=\"match_parent\"

android:paddingBottom=\"@dimen/activity_vertical_margin\" android:paddingLeft=\"@dimen/activity_horizontal_margin\" android:paddingRight=\"@dimen/activity_horizontal_margin\" android:paddingTop=\"@dimen/activity_vertical_margin\" tools:context=\".MainActivity\" >

android:id=\"@+id/textView1\"

android:layout_width=\"wrap_content\" android:layout_height=\"wrap_content\" android:text=\"@string/hello_world\" />

android:id=\"@+id/iv\"

android:layout_width=\"wrap_content\" android:layout_height=\"wrap_content\" />

======-=========================-===========

然后到主运行类MainActivity.java文件中写代码============ package com.example.a07072client;

import java.io.BufferedOutputStream; import java.io.File;

import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream;

import java.net.MalformedURLException; import java.net.URL;

import android.app.Activity; import android.graphics.Bitmap;

import android.graphics.BitmapFactory; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log;

import android.widget.ImageView;

public class MainActivity extends Activity {

Handler hd = new Handler() {

public void handleMessage(android.os.Message msg) {//接收消息的方

// 从消息队列读出

Bundle data = msg.getData();//从发过来的消息msg中getdata而拿到Bitmap bit = data.getParcelable(\"img\");//与put进去相对应这里是Log.e(\"\", \"bit \" + bit);

iv.setImageBitmap(bit);//把拿到的Bitmap对象设给iv==

@Override

protected void onCreate(Bundle savedInstanceState) { }

super.onCreate(savedInstanceState);

iv = (ImageView) findViewById(R.id.iv);//把组件设给布局==2 new Thread(r).start();//开启线程===3

setContentView(R.layout.activity_main);//把布局设给模拟器显示出来==1 // 2 -- 下载 建议开启新线程 // 4 -- 必须开启 ImageView iv;

法====

Bundle对象==== 取get===

};

}

Runnable r = new Runnable() {

@Override

public void run() {

Bitmap bit = down();//调用下面的下载图片的down()方法==== Message msg = hd.obtainMessage();

//他是从消息池中拿来一个msg 不需要另开辟空间new【效率低】,obtianmessage可以循环利用;

};

// 下载 -- 加权限 internet Bitmap down() {

//自己的这张图片放到本地的,用浏览器可以直接访问的====-========= //String path = \"http://192.168.1.62:8080/test/imgs/a.jpg\"; String path = }

Bundle bundle = new Bundle();//拿到Bundle对象的实例=== bundle.putParcelable(\"img\", bit);//key是img 值是bit 加到msg.setData(bundle);//把bundle放在Message对象中=== hd.sendMessage(msg);//发送出去== Log.e(\"\", \"send\");

Bundle中==

//=======================下面是下载图片的方法========-==============

\"http://10.207.113.121:8080/a07072server/img/k1.jpg\";//我电脑ip【这里不可以用localhost的】 // String path =

\"http://photocdn.sohu.com/20130514/Img375886304.jpg\";//我电脑ip【这里不可以用localhost的】

InputStream os = null;//放到外边,因为等一下要关闭的=== try {

URL url = new URL(path);//拿到url对象==== // 连接 --- 得到输入流

os = url.openStream();//根据url拿到输入流【这一句做了下边2句的事】// HttpURLConnection con = (HttpURLConnection) // os = con.getInputStream(); Log.e(\"\", \"os : \" + os);

Bitmap bf = BitmapFactory.decodeStream(os);//然后用

===

url.openConnection();

BitmapFactory对象去流里拿图片===

return bf;//返回这个图片===

} catch (MalformedURLException e) {

e.printStackTrace();

}

}

} catch (IOException e) { }

return null;//如果没图片调用这个方法就返回null。

e.printStackTrace(); try { }

if (os != null)

os.close();

} finally {

} catch (IOException e) {

e.printStackTrace();

=========-===================-======================-===============

就这样先运行服务端,然后运行客户端这边,就可以把服务端的图片传到客户端并且在客户端显示到模拟器上=============-==============

/////////////////////////////////////////////////////////////////////////////////////////////////

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- wuhaninfo.cn 版权所有

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务