栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android中实现下载URL地址的网络资源的实例分享

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android中实现下载URL地址的网络资源的实例分享

通过URL来获取网络资源并下载资源简单实例:

package com.android.xiong.urltest; 
 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.net.MalformedURLException; 
import java.net.URL; 
 
import android.app.Activity; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.Bundle; 
import android.os.Handler; 
import android.os.Message; 
import android.view.Menu; 
import android.widget.ImageView; 
 
public class MainActivity extends Activity { 
  ImageView show; 
  Bitmap bitmap; 
  Handler handler = new Handler() { 
 
    @Override 
    public void handleMessage(Message msg) { 
      if (msg.what == 0x123) { 
 // 使用ImageView显示该图片 
 show.setImageBitmap(bitmap); 
 
      } 
    } 
 
  }; 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    show = (ImageView) findViewById(R.id.show); 
 
    new Thread() { 
 
      @Override 
      public void run() { 
 // 定义一个URL对象 
 URL url; 
 try { 
   url = new URL( 
"http://img1.gtimg.com/news/pics/hv1/37/195/1468/95506462.jpg"); 
   // 打开该URL的资源输入流 
   InputStream is = url.openStream(); 
   // 从InputStream中解析出图片 
   bitmap = BitmapFactory.decodeStream(is); 
   // 发送消息 
   handler.sendEmptyMessage(0x123); 
   is.close(); 
   // 再次打开RL对应的资源输入流 
   is = url.openStream(); 
   // 打开手机文件对应的输出流 
   OutputStream os = openFileOutput("KEQIANG.JPG", MODE_APPEND); 
   byte[] buff = new byte[1024]; 
   int hasRead = 0; 
   // 将URL资源下载到本地 
   while ((hasRead = is.read(buff)) > 0) { 
     os.write(buff, 0, hasRead); 
   } 
   is.close(); 
   os.close(); 
 } catch (MalformedURLException e) { 
   // TODO Auto-generated catch block 
   e.printStackTrace(); 
 } catch (IOException e) { 
   // TODO Auto-generated catch block 
   e.printStackTrace(); 
 } 
 
      } 
 
    }.start(); 
  } 
 
  @Override 
  public 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; 
  } 
 
} 
 
 
   
 
 

网络资源多线程下载:

package com.example.threaddown; 
 
import java.util.Timer; 
import java.util.TimerTask; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.os.Handler; 
import android.os.Message; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ProgressBar; 
 
public class MainActivity extends Activity { 
 
  EditText url; 
  EditText target; 
  Button downBn; 
  ProgressBar bar; 
  DownUtil downUtil; 
  private int mDownStatus; 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    // 获取程序界面中的三个界面控制 
    url = (EditText) findViewById(R.id.url); 
    target = (EditText) findViewById(R.id.target); 
    downBn = (Button) findViewById(R.id.downBn); 
    bar = (ProgressBar) findViewById(R.id.br); 
    // 创建一个Handler对象 
    final Handler handler = new Handler() { 
 
      @Override 
      public void handleMessage(Message msg) { 
 if (msg.what == 0x123) { 
   bar.setProgress(mDownStatus); 
 } 
      } 
 
    }; 
    downBn.setonClickListener(new onClickListener() { 
 
      @Override 
      public void onClick(View v) { 
 // 初始化DownUtil对象 
 downUtil = new DownUtil(url.getText().toString(), target 
     .getText().toString(), 6); 
 new Thread() { 
 
   @Override 
   public void run() { 
     try { 
// 开始下载 
downUtil.download(); 
 
     } catch (Exception e) { 
e.printStackTrace(); 
     } 
     // 定义每秒调度获取一次系统的完成进度 
     final Timer timer = new Timer(); 
     timer.schedule(new TimerTask() { 
 
@Override 
public void run() { 
  // 获取下载任务的完成比例 
  double completeRate = downUtil 
      .getCompleteRate(); 
  mDownStatus = (int) (completeRate * 1000); 
  // 发送消息通知届满更新的进度条 
  handler.sendEmptyMessage(0x123); 
  // 下载完成之后取消任务进度 
  if (mDownStatus >= 100) { 
    timer.cancel(); 
  } 
} 
     }, 0, 1000); 
   } 
 
 }.start(); 
      } 
    }); 
  } 
 
  @Override 
  public 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; 
  } 
 
} 
package com.example.threaddown; 
 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.RandomAccessFile; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 
 
public class DownUtil { 
 
  // 定义下载资源的路径 
  private String path; 
  // 指定所下载的文件的保存位置 
  private String targetFile; 
  // 定义需要使用多少线程下载资源 
  private int threadNum; 
  // 定义下载的线程对象 
  private DownThread[] threads; 
  // 定义下载的文件总大小 
  private int fileSize; 
 
  public DownUtil(String path, String targetFile, int threadNum) { 
    this.path = path; 
    this.targetFile = targetFile; 
    this.threadNum = threadNum; 
  } 
 
  public void download() throws IOException { 
    URL url = new URL(path); 
    HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
    conn.setConnectTimeout(5000); 
    conn.setRequestMethod("GET"); 
    conn.setRequestProperty("Accept", "**"); 
 conn.setRequestProperty("Accept-Language", "zh-CN"); 
 conn.setRequestProperty("Charset", "UTF-8"); 
 conn.setRequestProperty("Connection", "Keep-Alive"); 
 InputStream in = conn.getInputStream(); 
 in.skip(startPos); 
 int hasRead = 0; 
 byte[] buffer = new byte[1024]; 
 // 读取网络数据,并写入本地文件 
 while (length < currentPartsSize 
     && (hasRead = in.read(buffer)) > 0) { 
   currentPart.write(buffer, 0, hasRead); 
   // 累计该线程下载的总大小 
   length += hasRead; 
 } 
 currentPart.close(); 
 in.close(); 
 
      } catch (Exception e) { 
 e.printStackTrace(); 
      } 
 
    } 
 
  } 
 
} 


 
 
   
 
   
 
  
 
 
 
 
 
 
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/162664.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号