android 获取服务端图片
来源:互联网 发布:c语言一共有多少个函数 编辑:程序博客网 时间:2024/06/05 01:04
开发工具 java EE 同时自己在官网上下载ADT 以及对应版本的SDK。
服务端一般是将服务端发送到tomcat上,然后再tomcat上运行。这里不再赘述。
客户端下载图片代码
public class MainActivity extends ActionBarActivity {ImageView imageView;EditText editText;String path;@Overrideprotected 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() {@Overridepublic 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>
0 0
- android 获取服务端图片
- Android自定义ListView图片从服务端获取
- Android 获取本地图片&图片上传至服务端
- Android 拍照剪裁,获取相册图片,上传照片至服务端
- android 上传图片 客户端与 服务端 方案总结 (包括获取当前的进度)
- 一步一步学会http获取tomcat服务端的图片,在android客户端显示
- 工作中技术难点一:android客户端获取服务端验证码图片
- Android如何压缩图片上传服务端
- android端上传图片到服务端
- Android获取服务端数据解析保护
- 利用DataGramPacket和DtaGramSocket编写的从服务端获取图片
- android应用——图片获取(调用相机或者从相册获取)、裁剪并上传至服务端,适用于头像设置等
- Android获取图片资源
- Android获取图片信息
- android获取图片数据
- android 获取图片
- android 异步获取图片
- Android 获取手机图片
- 跳舞毯
- 数据库关于同一张表不同条件取出数量的横向添加sql语句
- vim基本命令
- CSS:响应式下的折叠菜单(条纹式)
- Good Bye 2014
- android 获取服务端图片
- 如何给Apache Pig自定义UDF函数?
- 循环水处理剂:游泳池水处理药剂—消毒剂的使用方法与用量
- 【DP】 codeforces 500D New Year Santa Network
- mac vim处理dos换行符^M
- Mac OS10.10.1环境下eclipse中的Android device chooser找不到真机的解决方案
- C#+OpenGL3编程之第一个三角形
- 计算整数的位数
- 【安卓笔记】数据适配器(adapter)中的观察者模式