查看网络图片
来源:互联网 发布:民生银行网络金融部 编辑:程序博客网 时间:2024/06/05 21:53
网络图片查看
一、相关知识了解:
1、什么是进程?
当一个程序开始运行时,它就是一个进程,进程包括运行中的程序和程序所使用到的内存和系统资源。
而一个进程又是由多个线程所组成的。
2、什么是多线程?
多线程是指程序中包含多个执行流,即在一个程序中可以同时运行多个不同的线程来执行不同的任务,也就是说允许单个程序创建多个并行执行的线程来完成各自的任务。
3、多线程的好处:
可以提高CPU的利用率。在多线程程序中,一个线程必须等待的时候,CPU可以运行其它的线程而不是等待,这样就大大提高了程序的效率。
二、实现过程如下:
1、页面布局activity_mian.xml:
2、
3、采用匿名内部类的方法:
4.程序的效果图如下:
一、相关知识了解:
1、什么是进程?
当一个程序开始运行时,它就是一个进程,进程包括运行中的程序和程序所使用到的内存和系统资源。
而一个进程又是由多个线程所组成的。
2、什么是多线程?
多线程是指程序中包含多个执行流,即在一个程序中可以同时运行多个不同的线程来执行不同的任务,也就是说允许单个程序创建多个并行执行的线程来完成各自的任务。
3、多线程的好处:
可以提高CPU的利用率。在多线程程序中,一个线程必须等待的时候,CPU可以运行其它的线程而不是等待,这样就大大提高了程序的效率。
二、实现过程如下:
1、页面布局activity_mian.xml:
<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" 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" > <ImageView android:id="@+id/ivImage" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="1" /> <EditText android:id="@+id/etImageUrl" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:hint="请输入图片的地址" android:text="@string/address"/> <Button android:id="@+id/btnView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:background="@drawable/button_bg" android:onClick="viewImage" android:text="浏览" /></LinearLayout>
2、
public class MainActivity extends Activity { private EditText etImageUrl; private ImageView ivImage; public static final int SHOWIMAGE=1; private Handler handler=new Handler(){ public void handleMessage(android.os.Message msg) { switch (msg.what) {case SHOWIMAGE:Bitmap bitmap=(Bitmap) msg.obj;ivImage.setImageBitmap(bitmap);break;default:break;} }; };@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initViews();}private void initViews() { etImageUrl=(EditText) findViewById(R.id.etImageUrl); ivImage=(ImageView) findViewById(R.id.ivImage);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;} //1:初始程序:/*public void viewImage(View view){String path=etImageUrl.getText().toString();//把图片路径转换成字符串if(TextUtils.isEmpty(path)){/* * question:对于一个UI界面中,当判断用户是否输入用户名或密码时,我们常用TextUtils.isEmpty()方法来判断;但有时也可以用这个equals()方法,都可以来判断EditText中是否为空,但有时很纠结,不知道这两种方法中哪个比较好?为什么? answer:仔细读官方的API: Returns true if the string is null or 0-length. 因为你从EditText返回的是一个变量。如果这个变量本身为null值,那么你掉它的equals方法是要报错的。但是如果你调用TextUtils.isEmpty() 把这个变量作为参数传进去。 只要这个参数为空或者为"",都会返回真。所以,用官方给的更加严谨。 而且,也十分方便。因为你单独去判断你还不是要写一个if语句判断。返回的还是一个boolean值Toast.makeText(this, R.string.NOnull, Toast.LENGTH_LONG).show();//如果输入路径为空,就弹出Toast}else{//不为空,连接服务器,请求获得图片try{URL url=new URL(path);//发出http请求HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();httpURLConnection.setRequestMethod("GET");//设置提交方式//设置连接超时时间httpURLConnection.setConnectTimeout(5000);//这时,我们设置为超时时间为5秒,如果5秒内不能连接就被认为是有错误发生.int responsecode=httpURLConnection.getResponseCode();if(responsecode==200){InputStream inputstream=httpURLConnection.getInputStream();Bitmap bitmap=BitmapFactory.decodeStream(inputstream);ivImage.setImageBitmap(bitmap);}else{Toast.makeText(this, R.string.error, Toast.LENGTH_LONG).show();}}catch(MalformedURLException e){e.printStackTrace();}catch(IOException E){E.printStackTrace();}}*/public void viewImage(View view){final String imageUrl=etImageUrl.getText().toString();if(TextUtils.isEmpty(imageUrl)){Toast.makeText(this, "图片路径不能为空", Toast.LENGTH_LONG).show();}else{new Thread(){public void run() {try {URL url=new URL(imageUrl);HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection(); httpURLConnection.setRequestMethod("GET"); httpURLConnection.setConnectTimeout(5000); int responseCode=httpURLConnection.getResponseCode(); if(responseCode==200){ InputStream inputStream=httpURLConnection.getInputStream(); Bitmap bitmap=BitmapFactory.decodeStream(inputStream); Message message=new Message(); message.what=SHOWIMAGE; message.obj=bitmap; //ivImage.setImageBitmap(bitmap); handler.sendMessage(message); }else{ Toast.makeText(MainActivity.this, "显示图片失败", Toast.LENGTH_LONG).show(); }} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}.start();}}}
3、采用匿名内部类的方法:
public void viewImage(View view){ final String imageUrl=etImageUrl.getText().toString(); if(TextUtils.isEmpty(imageUrl)){ Toast.makeText(this, "图片路径不能为空", Toast.LENGTH_LONG).show(); }else{ new Thread(){ public void run() { try { //在处理的过程中,必须进行异常处理 URL url=new URL(imageUrl); HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection(); httpURLConnection.setRequestMethod("GET"); httpURLConnection.setConnectTimeout(5000); int responseCode=httpURLConnection.getResponseCode(); if(responseCode==200){ InputStream inputStream=httpURLConnection.getInputStream(); Bitmap bitmap=BitmapFactory.decodeStream(inputStream); Message message=new Message(); message.what=SHOWIMAGE; message.obj=bitmap; //ivImage.setImageBitmap(bitmap); handler.sendMessage(message); }else{ Toast.makeText(MainActivity.this, "显示图片失败", Toast.LENGTH_LONG).show(); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }.start(); } }
4.程序的效果图如下:
0 0
- 网络图片查看
- 网络图片查看器
- 网络图片查看器
- 查看网络图片
- 网络图片查看器
- 网络图片查看器
- 网络图片查看器
- *******************网络图片查看器*************
- 网络图片查看器
- 网络图片查看器
- Android查看网络图片
- 网络图片查看器
- 网络图片查看
- android查看网络图片
- 网络图片查看
- 查看网络图片
- 查看网络图片
- 查看网络图片
- 关于KM算法
- GRBL四:GRBL框架解析
- 利用sed命令在匹配某特定字符串的行尾添加字符串
- 在windows中安装Hadoop
- em(倍)与px的区别
- 查看网络图片
- 命令行参数
- vc 防止多次运行同一个程序的方法
- [译]JavaScript中:表达式和语句的区别
- iOS开发UI篇—IOS开发中Xcode的一些使用技巧
- TestNg JAVA 自动化单元测试框架Demo
- leetcode 37:Sudoku Solver
- Mecanim动画编辑器 - 添加动画层实现并行动作
- tomcat下配置https环境