Android Http底子:图片下载并体现和WebView的应用

打印 上一主题 下一主题

主题 889|帖子 889|积分 2667

<RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”
   
  1. xmlns:tools="http://schemas.android.com/tools"
复制代码
  
  1. android:layout_width="match_parent"
复制代码
  
  1. android:layout_height="match_parent"
复制代码
  
  1. android:paddingLeft="@dimen/activity_horizontal_margin"
复制代码
  
  1. android:paddingRight="@dimen/activity_horizontal_margin"
复制代码
  
  1. android:paddingTop="@dimen/activity_vertical_margin"
复制代码
  
  1. android:paddingBottom="@dimen/activity_vertical_margin"
复制代码
  
  1. tools:context=".MainActivity">
复制代码
   
  1. <ImageView
复制代码
  
  1.     android:id="@+id/image"
复制代码
  
  1.     android:layout_width="fill_parent"
复制代码
  
  1.     android:layout_height="wrap_content"></ImageView>
复制代码
   
  1. <WebView
复制代码
  
  1.     android:id="@+id/wedView"
复制代码
  
  1.     android:layout_width="fill_parent"
复制代码
  
  1.     android:layout_height="fill_parent"
复制代码
  
  1.     android:layout_below="@id/image"></WebView>
复制代码
       MainActivity:
   
     /**
   

  • 在Android上发送HTTP哀求的方式一般有两种,HttpURLConnection和HttpClient
    */
     public class MainActivity extends ActionBarActivity {
   
  1. private WebView webView;
复制代码
  
  1. private ImageView imageView;
复制代码
   
  1. public static final int SHOW_WEDVIEW = 0;
复制代码
  
  1. private Handler handler = new Handler();
复制代码
   
  1. @Override
复制代码
  
  1. protected void onCreate(Bundle savedInstanceState) {
复制代码
  
  1.     super.onCreate(savedInstanceState);
复制代码
  
  1.     setContentView(R.layout.activity_main);
复制代码
   
  1.     webView = (WebView) findViewById(R.id.wedView);
复制代码
  
  1.     imageView = (ImageView) findViewById(R.id.image);
复制代码
   
  1.     //通过网络加载图片
复制代码
  
  1.     new ImageThread("http://gb.cri.cn/mmsource/images/2013/02/22/35/14607758026320856623.jpg",imageView,handler).start();
复制代码
   
  1.     //在WebView中显示百度主页
复制代码
  
  1.     new HttpThread("http://www.baidu.com",webView,handler).start();
复制代码
  
  1. }
复制代码
    }





     ImageThread(加载图片的线程):
   
     public class ImageThread extends Thread {
   
  1. //声明要传递的参数
复制代码
  
  1. private String url;
复制代码
  
  1. private ImageView imageView;
复制代码
  
  1. private Handler handler;
复制代码
   
  1. //创建构造方法,对参数进行初始化
复制代码
  
  1. public ImageThread(String url, ImageView imageView, Handler handler) {
复制代码
  
  1.     this.url = url;
复制代码
  
  1.     this.imageView = imageView;
复制代码
  
  1.     this.handler = handler;
复制代码
  
  1. }
复制代码
   
  1. @Override
复制代码
  
  1. public void run() {
复制代码
  
  1.     try {
复制代码
  
  1.         //定义一个URL对象
复制代码
  
  1.         URL httpUrl = new URL(url);
复制代码
  
  1.         //获取HttpURLConnection的实例,表示到URL所引用的远程对象的连接
复制代码
  
  1.         HttpURLConnection connection = (HttpURLConnection) httpUrl.openConnection();
复制代码
  
  1.         //设置读取超时时间
复制代码
  
  1.         connection.setReadTimeout(5000);
复制代码
  
  1.         //设置请求方法,GET表示希望从服务器那里获取数据,而POST则表示提交数据给服务器
复制代码
  
  1.         connection.setRequestMethod("GET");
复制代码
  
  1.         //设置该URLConnection的doOutput请求头字段的值
复制代码
  
  1.         connection.setDoInput(true);
复制代码
   
  1.         //用时间作为下载的图片的文件名
复制代码
  
  1.         String fileName = String.valueOf(System.currentTimeMillis());
复制代码
  
  1.         File downloadFile = null;
复制代码
  
  1.         InputStream inputStream = connection.getInputStream();
复制代码
  
  1.         //要把文件写到SD卡,所以用FileOutputStream
复制代码
  
  1.         FileOutputStream fileOutputStream = null;
复制代码
   
  1.         //判断SD卡是否存在,存在就创建文件
复制代码
  
  1.         if (Environment.getExternalStorageState().equals(
复制代码
  
  1.                 Environment.MEDIA_MOUNTED)) {
复制代码
  
  1.             File parent = Environment.getExternalStorageDirectory();
复制代码
  
  1.             downloadFile = new File(parent,fileName);
复制代码
  
  1.             fileOutputStream = new FileOutputStream(downloadFile);
复制代码
  
  1.         }
复制代码
   
  1.         //创建一个2K的缓冲区
复制代码
  
  1.         byte[] bytes = new byte[2 * 1024];
复制代码
  
  1.         int length;
复制代码
   
  1.         if (fileOutputStream != null) {
复制代码
  
  1.             while ((length = inputStream.read(bytes)) != -1) {
复制代码
  
  1.                 fileOutputStream.write(bytes,0,length);
复制代码
  
  1.             }
复制代码
  
  1.         }
复制代码
   
  1.         final Bitmap bitmap = BitmapFactory.decodeFile(downloadFile.getAbsolutePath());
复制代码
   
  1.         //通过handler去更新UI
复制代码
  
  1.         handler.post(new Runnable() {
复制代码
  
  1.             @Override
复制代码
  
  1.             public void run() {
复制代码
  
  1.                 imageView.setImageBitmap(bitmap);
复制代码
  
  1.             }
复制代码
  
  1.         }
  2. );
复制代码
   
  1.     }
  2. catch (MalformedURLException e) {
复制代码
  
  1.         e.printStackTrace();
复制代码
  
  1.     }
  2. catch (IOException e) {
复制代码
  
  1.         e.printStackTrace();
复制代码
  
  1.     }
复制代码
   
  1. }
复制代码
   }





       HttpThread(加载WebView的线程):
     
     public class HttpThread extends Thread {
   
  1. //声明要传递的参数
复制代码
  
  1. private String url;
复制代码
  
  1. private WebView webView;
复制代码
  
  1. private Handler handler;
复制代码
   
  1. //创建构造方法,对参数进行初始化
复制代码
  
  1. public HttpThread(String url, WebView webView, Handler handler) {
复制代码
  
  1.     this.url = url;
复制代码
  
  1.     this.webView = webView;
复制代码
  
  1.     this.handler = handler;
复制代码
  
  1. }
复制代码
   
  1. @Override
复制代码
  
  1. public void run() {
复制代码
   
  1.     try {
复制代码
  
  1.         //定义一个URL对象
复制代码
  
  1.         URL httpUrl = new URL(url);
复制代码
  
  1.         //获取HttpURLConnection的实例,表示到URL所引用的远程对象的连接
复制代码
  
  1.         HttpURLConnection connection = (HttpURLConnection) httpUrl.openConnection();
复制代码
  
  1.         //设置读取超时时间
复制代码
  
  1.         connection.setReadTimeout(5000);
复制代码
  
  1.         //设置请求方法,GET表示希望从服务器那里获取数据,而POST则表示提交数据给服务器
复制代码
  
  1.         connection.setRequestMethod("GET");
复制代码
  
  1.         final StringBuffer stringBuffer = new StringBuffer();
复制代码
  
  1.         /*  getInputStream():返回该URLConnection对应的输入流,用于获取URLConnection响应的内容
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

钜形不锈钢水箱

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表