日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android 下载进度条代码实现,Android 文件下载进度条的实现

發布時間:2024/10/12 Android 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 下载进度条代码实现,Android 文件下载进度条的实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Android 中很多地方都需要用到線程下載。下面我謝了個簡單的下載圖片的demo。望對你們有到幫助.

首先,配置權限,

在?AndroidManifest.xml里面的application節點下面配置需要用到的權限

權限添加完成,布局如下:

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

android:id="@+id/btn_1"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="點擊下載圖片"

/>

android:id="@+id/tv"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="" />

android:id="@+id/down_pb"

style="?android:attr/progressBarStyleHorizontal"

mce_style="?android:attr/progressBarStyleHorizontal"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:max="100" />

接來下就是寫代碼了,代碼其實很簡單,話不多說,我直接上傳:

package com.lh.thread;

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.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.ProgressBar;

import android.widget.TextView;

import android.widget.Toast;

public class Main extends Activity {

/** Called when the activity is first created. */

ProgressBar mPb;

TextView mTv;

Button mBtn;

int fileSize;

int downLoadFileSize;

String fileEx, fileNa, filename;

private Handler handler = new Handler() {

@Override

public void handleMessage(Message msg) {// 定義一個Handler,用于處理下載線程與UI間通訊

if (!Thread.currentThread().isInterrupted()) {

switch (msg.what) {

case 0:

mPb.setMax(fileSize);

case 1:

mPb.setProgress(downLoadFileSize);

int result = downLoadFileSize * 100 / fileSize;

mTv.setText(result + "%");

break;

case 2:

Toast.makeText(Main.this, "文件下載完成", 1).show();

break;

case -1:

String error = msg.getData().getString("error");

Toast.makeText(Main.this, error, 1).show();

break;

}

}

super.handleMessage(msg);

}

};

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

//初始化控件

mPb = (ProgressBar) findViewById(R.id.down_pb);

mTv = (TextView) findViewById(R.id.tv);

mBtn=(Button) findViewById(R.id.btn_1);

mBtn.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

//啟動線程進行下載

new Thread() {

public void run() {

try {

down_file(

"http://att.bbs.duowan.com/forum/201310/23/151916t9kdya2gia0a21la.jpg",

"/sdcard/");

// 下載文件,參數:第一個URL,第二個存放路徑

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}.start();

}

});

}

/**

* 此方法描述的是:下載文件并保存

* @param url

* @param path

* @throws IOException void

*/

public void down_file(String url, String path) throws Exception {

Log.i("AAAAAA", "~~~~~~~~~存儲路徑是"+path);

// 下載函數

filename = url.substring(url.lastIndexOf("/") + 1);

// 獲取文件名

URL myURL = new URL(url);

URLConnection conn = myURL.openConnection();

conn.connect();

InputStream is = conn.getInputStream();

this.fileSize = conn.getContentLength();// 根據響應獲取文件大小

if (this.fileSize <= 0)

throw new RuntimeException("無法獲知文件大小 ");

if (is == null)

throw new RuntimeException("stream is null");

FileOutputStream fos = new FileOutputStream(path + filename);

// 把數據存入路徑+文件名

byte buf[] = new byte[1024];

downLoadFileSize = 0;

sendMsg(0);

do {

// 循環讀取

int numread = is.read(buf);

if (numread == -1) {

break;

}

fos.write(buf, 0, numread);

downLoadFileSize += numread;

sendMsg(1);// 更新進度條

} while (true);

sendMsg(2);// 通知下載完成

try {

is.close();

} catch (Exception ex) {

Log.e("tag", "error: " + ex.getMessage(), ex);

}

}

private void sendMsg(int flag) {

Message msg = new Message();

msg.what = flag;

handler.sendMessage(msg);

}

}好了,進度條下載就完成了

總結

以上是生活随笔為你收集整理的android 下载进度条代码实现,Android 文件下载进度条的实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。