layout文件布局
<?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" >    <Button        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:id="@+id/bt"        android:text="开始下载文件" />    <ProgressBar        android:id="@+id/pb"        android:layout_width="fill_parent"        style="?android:attr/progressBarStyleHorizontal"        android:layout_height="wrap_content" />    <ImageView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/img"/></LinearLayout>

文件工具类
package com.spring.sky.download;import java.io.File;import android.content.Context;import android.os.Environment;import android.util.Log;/** * 文件工具类 * @author spring sky * Email:vipa1888@163.com * QQ:840950105 * name:石明政 * */public class FileUtil {/** * 检验SDcard状态 * @return boolean */public static boolean checkSDCard(){if(android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)){return true;}else{return false;}}/** * 保存文件文件到目录 * @param context * @return  文件保存的目录 */public static String setMkdir(Context context){String filePath;if(checkSDCard()){filePath = Environment.getExternalStorageDirectory()+File.separator+"myfile";}else{filePath = context.getCacheDir().getAbsolutePath()+File.separator+"myfile";}File file = new File(filePath);if(!file.exists()){boolean b = file.mkdirs();Log.e("file", "文件不存在  创建文件    "+b);}else{Log.e("file", "文件存在");}return filePath;}}

界面上的逻辑处理
package com.spring.sky.download;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.URL;import java.net.URLConnection;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.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;import android.widget.ProgressBar;import android.widget.Toast;/** * 文件下载界面 * @author spring sky * Email:vipa1888@163.com * QQ:840950105 * name:石明政 * */public class IndexActivity extends Activity implements OnClickListener{private static final int DOWNLOAD_PREPARE = 0;private static final int DOWNLOAD_WORK = 1;private static final int DOWNLOAD_OK = 2;private static final int DOWNLOAD_ERROR =3;private static final String TAG = "IndexActivity";private Button bt ;private ProgressBar pb;private ImageView img;/** * 需要下载的文件 * 注意:在模拟器上面使用无法解析有域名的主机 */private String url = "http://61.155.165.32/shuixiyue/pic/item/f141247490d0e96fb251b963.jpg";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);bt = (Button) this.findViewById(R.id.bt);bt.setOnClickListener(this);pb = (ProgressBar) this.findViewById(R.id.pb);pb.setVisibility(ProgressBar.INVISIBLE);img =(ImageView) this.findViewById(R.id.img);}/** * 按钮点击事件 */@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.bt:Toast.makeText(this, "开始下载文件", Toast.LENGTH_SHORT).show();new Thread(){@Overridepublic void run() {downloadFile();super.run();}}.start();break;}}/** * 文件下载 */private void downloadFile(){try {URL u = new URL(url);URLConnection conn = u.openConnection();conn.connect();InputStream is = conn.getInputStream();fileSize = conn.getContentLength();if(fileSize<1||is==null){sendMessage(DOWNLOAD_ERROR);}else{sendMessage(DOWNLOAD_PREPARE);FileOutputStream fos = new FileOutputStream(getPath());byte[] bytes = new byte[1024];int len = -1;while((len = is.read(bytes))!=-1){fos.write(bytes, 0, len);downloadSize+=len;sendMessage(DOWNLOAD_WORK);}sendMessage(DOWNLOAD_OK);is.close();fos.close();}} catch (Exception e) {sendMessage(DOWNLOAD_ERROR);e.printStackTrace();} }/** * 文件一共的大小 */int fileSize = 0;/** * 已经下载的大小 */int downloadSize = 0;/** * handler处理消息 */private Handler handler = new Handler(){@Overridepublic void handleMessage(Message msg) {switch (msg.what) {case DOWNLOAD_PREPARE:Toast.makeText(IndexActivity.this, "下载准备", Toast.LENGTH_SHORT).show();pb.setVisibility(ProgressBar.VISIBLE);Log.e(TAG, "一共:"+fileSize);pb.setMax(fileSize);break;case DOWNLOAD_WORK:Log.e(TAG, "已下载:"+downloadSize);pb.setProgress(downloadSize);int res = downloadSize*100/fileSize;bt.setText("已下载:"+res+"%");break;case DOWNLOAD_OK:try {if(getPath().endsWith(".jpg")||getPath().endsWith(".png")){FileInputStream fis = new FileInputStream(getPath());img.setImageBitmap(BitmapFactory.decodeStream(fis));}downloadSize = 0;fileSize = 0;} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}Toast.makeText(IndexActivity.this, "下载完成", Toast.LENGTH_SHORT).show();break;case DOWNLOAD_ERROR:Toast.makeText(IndexActivity.this, "下载出错", Toast.LENGTH_SHORT).show();break;}super.handleMessage(msg);}};/** * 得到文件的保存路径 * @return * @throws IOException */private String getPath() throws IOException{String path = FileUtil.setMkdir(this)+File.separator+url.substring(url.lastIndexOf("/")+1);return path;}/** * 给hand发送消息 * @param what */private void sendMessage(int what){Message m = new Message();m.what = what;handler.sendMessage(m);}}


转自: http://blog.csdn.net/vipa1888/article/details/7208956
非常感谢 哈哈

更多相关文章

  1. EditeText标签字体大小设置 是否可编辑
  2. Android安装以及Eclipse插件(Google Android) ,在Android 中goog
  3. android计算器布局界面——基础编
  4. android:shape的使用 (android用xml文件生成图像控件)
  5. [Android UI界面] Android UI 设计准则
  6. android 自定义View 在布局文件中写法
  7. Android上传下载文件(图片)
  8. android界面模糊到清晰,效果去除
  9. android 录像/打开video文件

随机推荐

  1. 解决Android(安卓)sdk更新不能下载或下载
  2. android代码库之textview跑马灯效果
  3. 老罗Android开发视频教程 (android常用布
  4. 修改android升级系统后启动系统,提示andro
  5. Professional Android(安卓)2 Developmen
  6. 【Android(安卓)Studio使用教程4】Androi
  7. android selector 背景选择器的使用, butt
  8. Android(安卓)入门前言之 --布局
  9. Android开发软件
  10. Android面试题整理(中)