网络通信之网络图片获取 并显示屏幕上
来源:互联网 发布:域名修改dns多久生效 编辑:程序博客网 时间:2024/05/23 11:58
1.主MainActivity 类代码如下:
public class MainActivity extends Activity {private EditText editText;private ImageView imageView;private static final String TAG="MainActivity";private Button button;private byte []data;private Handler handler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case 0: Bitmap bitmap=BitmapFactory.decodeByteArray(data, 0, data.length);imageView.setImageBitmap(bitmap);break; } }; }; @Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());// StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build());setContentView(R.layout.activity_main);editText =(EditText) findViewById(R.id.path);imageView=(ImageView) findViewById(R.id.ImageView); button=(Button) findViewById(R.id.button);button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {new Thread() { @Override public void run() { // TODO Auto-generated method stub super.run(); String path=editText.getText().toString();try {data=NetTool.getImage(path);} catch (Exception e) {// TODO Auto-generated catch blockToast.makeText(MainActivity.this, "获取图片失败", 1).show();e.printStackTrace();}Message msg = handler.obtainMessage(); msg.what = 0; handler.sendMessage(msg); } }.start(); }});}}
2.NetTool类代码:
public class NetTool {public static byte[] getImage(String path) throws Exception{URL url = new URL(path);HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setConnectTimeout(5000);conn.setRequestMethod("GET");if(conn.getResponseCode() == 200){InputStream inStream = conn.getInputStream();return read(inStream);}return null;}public static byte[] read(InputStream inStream) throws Exception{ByteArrayOutputStream outStream = new ByteArrayOutputStream();byte[] buffer = new byte[1024];int len = 0;while( (len = inStream.read(buffer)) != -1){outStream.write(buffer, 0, len);}inStream.close();return outStream.toByteArray();}}
3.activity_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/path" /> <EditText android:id="@+id/path" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/aa" > </EditText> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button" > </Button> <ImageView android:id="@+id/ImageView" android:layout_width="wrap_content" android:layout_height="wrap_content" > </ImageView></LinearLayout>
4.string.xml
<resources> <string name="app_name">GetPicture</string> <string name="hello_world">Hello world!</string> <string name="menu_settings">Settings</string> <string name="title_activity_main">MainActivity</string> <string name="path">路径</string> <string name="button">获取图片</string> <string name="aa">http://a.hiphotos.baidu.com/image/pic/item/94cad1c8a786c9178f04af6fca3d70cf3bc75765.jpg</string> </resources>5.记得在清单文件里加权限:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
运行结果:
0 0
- 网络通信之网络图片获取 并显示屏幕上
- 网络通信之网页源码查看 并显示屏幕上
- 获取网络图片并显示
- android 获取网络图片并在Imageview上显示
- Android基础-获取网络上的图片并显示
- android 获取网络图片并在Imageview上显示
- android网络通信之获取网络图片
- android网络通信之获取网络图片
- swing获取网络图片,并压缩显示
- android从网络获取图片并显示
- [Android] 获取网络图片并显示
- android从网络中获得一张图片,并显示在屏幕上
- android从网络中获得一张图片,并显示在屏幕上
- 获取网络上的一张图片并显示在界面上
- 获取网络上的图片,并显示在界面上的简单实现
- android 网络图片按原始比例显示在屏幕上
- cocos2dx 获取网络图片并创建精灵显示到界面上
- 网络上获取图片
- PDO获取数据的方法fetch()、fetchAll()、setFetchMode()、bindColumn()
- Minimum Cost (poj 2516 求K次最小费用流)
- C语言之memset
- mac系统里spark单机环境的安装和配置
- 二叉树中两个结点的最近公共祖先(Lowest Common Ancestor of a Binary Tree)
- 网络通信之网络图片获取 并显示屏幕上
- FFT后的物理意义
- 1066. Root of AVL Tree (25)
- Eclipse导入外部项目问题总结 Target runtime com.genuitec.runtime.generic.jee6 is not defined
- 抽象一个分页的方法出来
- Android对图片的压缩读取和保存
- TCPDump抓取vlan/pppoe等数据帧的规则
- make版本不符合要求,不能编译android的解决方法
- Anonymous Inner Class (匿名内部类) 是否可以extends(继承)其它类,是否可以implements(实现)interface(接口)?