开发工具 java EE 同时自己在官网上下载ADT 以及对应版本的SDK。
服务端一般是将服务端发送到tomcat上,然后再tomcat上运行。这里不再赘述。
客户端下载图片代码
public class MainActivity extends ActionBarActivity {
ImageView imageView;
EditText editText;
String path;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView=(ImageView)this.findViewById(R.id.ImageView);
editText=(EditText)this.findViewById(R.id.EditText);
}
public void showimage(View v){
path=editText.getText().toString().trim();
new Thread(runnable).start();
}
Handler handler=new Handler(){
public void handleMessage(Message msg){
if(msg.what==1){
Bitmap bitmap=(Bitmap)msg.obj;
imageView.setImageBitmap(bitmap);
}
}
};
Runnable runnable=new Runnable() {
@Override
public void run() {
try {
Bitmap bitmap=getImageBitmap();
Message msg=new Message();
msg.obj=bitmap;
msg.what=1;
handler.sendMessage(msg);
} catch (IOException e) {
e.printStackTrace();
}
}
public Bitmap getImageBitmap() throws IOException{
URL url=new URL(path);
HttpURLConnection con=(HttpURLConnection) url.openConnection();
con.setConnectTimeout(4000);
con.setRequestMethod("GET");
if(con.getResponseCode() == 200){
InputStream stream=con.getInputStream();
Bitmap bitmap=BitmapFactory.decodeStream(stream);
return bitmap;
}
return null;
}
};
}
这里用 的是启动一个线程获取http链接。android 4.0以后,网络连接就不能放在主线程中了,否则会抛异常 。而android 4.0之前可以直接在主线程中获取
HttpURLConnection .
因为要联网 ,所以需要联网权限
<uses-permission android:name="android.permission.INTERNET"/>
布局代码如下
<LinearLayout 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:orientation="vertical"
>
<EditText
android:id="@+id/EditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="http://192.168.1.23:8080/ServerForPicture/hehe.jpg" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:onClick="showimage"
/>
<ImageView
android:id="@+id/ImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>