下載進度比例顯示
博客分類:
android
Android文件下載比例顯示
近幾天因爲在寫高清壁紙的服務器端(基於PHP+MySql),所以好幾天沒更新博客了,順便彙報一下高清壁紙的開發進度:服務器端已經改寫,原來是一條數據一條數據加,現在是Flash批量上傳圖片,自動添加數據(這是後臺,大家當然看不到啦)。另外,圖片數據都放到自己的虛擬主機上了,所以大家可能覺得這幾天下載圖片特別慢。原來放POCO,速度比較快,但是畢竟是免費的,不放心,怕哪天被人發現了大流量下載,把我帳號圖片刪了,到時哭都來不及,而且那樣也實現不了上傳自動添加數據。新版客戶端方面,目前多語言以及HVGA支持都已經完成,現在在做下載進度條(因爲服務器稍慢,怕有些性子急的同學等不了)。彙報完畢,開始今天學習。今天的這段代碼是網上找的,自己做了些小改,通過模擬器測試。文件下載進度條控制(就是爲了高清壁紙加個進度條),自己研究了好久,但是進度條只能顯示緩存寫入文件的進度,不能顯示下載進度。找了好久,終於找到一段用的代碼,所以記錄下來,大家分享。
佈局XML:
Xml代碼
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
/>
<ProgressBar android:id="@+id/down_pb"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="100"
style="?android:attr/progressBarStyleHorizontal"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
/>
<ProgressBar android:id="@+id/down_pb"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="100"
style="?android:attr/progressBarStyleHorizontal"
/>
</LinearLayout>
這個就不用解釋了吧,兩個控件,一個是TextView,一個是橫向條狀進度條
程序main.java:
Java代碼
package com.pocketdigi.download;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import org.apache.http.client.ClientProtocolException;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
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 pb;
TextView tv;
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:
pb.setMax(fileSize);
case 1:
pb.setProgress(downLoadFileSize);
int result = downLoadFileSize * 100 / fileSize;
tv.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);
pb=(ProgressBar)findViewById(R.id.down_pb);
tv=(TextView)findViewById(R.id.tv);
new Thread(){
public void run(){
try {
down_file("http://wallpaper.pocketdigi.com/upload/1/bigImage/1284565196.jpg","/sdcard/");
//下載文件,參數:第一個URL,第二個存放路徑
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
public void down_file(String url,String path) throws IOException{
//下載函數
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);
}
package com.pocketdigi.download;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import org.apache.http.client.ClientProtocolException;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
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 pb;
TextView tv;
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:
pb.setMax(fileSize);
case 1:
pb.setProgress(downLoadFileSize);
int result = downLoadFileSize * 100 / fileSize;
tv.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);
pb=(ProgressBar)findViewById(R.id.down_pb);
tv=(TextView)findViewById(R.id.tv);
new Thread(){
public void run(){
try {
down_file("http://wallpaper.pocketdigi.com/upload/1/bigImage/1284565196.jpg","/sdcard/");
//下載文件,參數:第一個URL,第二個存放路徑
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
public void down_file(String url,String path) throws IOException{
//下載函數
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);
}
LinearLayout按下(pressed)或獲取焦點(focused)時背景設置不同顏色或圖片
博客分類:
android
AndroidXML
Xml代碼
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myview"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:background="@drawable/myDrawable"
<SPAN style="COLOR: #ff0000"><SPAN style="TEXT-DECORATION: underline">android:clickable="true"</SPAN>
</SPAN>
>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="@color/black" /> <!-- focused -->
<item android:state_focused="true" android:state_pressed="true" android:drawable="@color/black" /> <!-- focused and pressed-->
<item android:state_pressed="true" android:drawable="@color/green" /> <!-- pressed -->
<item android:drawable="@color/black" /> <!-- default -->
</selector>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myview"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:background="@drawable/myDrawable"
android:clickable="true"
>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="@color/black" /> <!-- focused -->
<item android:state_focused="true" android:state_pressed="true" android:drawable="@color/black" /> <!-- focused and pressed-->
<item android:state_pressed="true" android:drawable="@color/green" /> <!-- pressed -->
<item android:drawable="@color/black" /> <!-- default -->
</selector>
千萬注意上面紅色標註的地方,一定要加這個屬性,不然不起作用,另外在color.xml中可以定義drawable和color等多種屬性,害我老是報找不到@color匹配的項,原來一直都在寫drawable,尼瑪!!!!!