package
com.src.fpkj.android.down;
import
java.io.File;
import
java.io.FileOutputStream;
import
java.io.InputStream;
import
java.io.OutputStream;
import
java.net.HttpURLConnection;
import
java.net.URL;
import
com.src.fpkj.android.R;
import
android.app.AlertDialog;
import
android.app.Dialog;
import
android.content.Context;
import
android.os.Environment;
import
android.os.Handler;
import
android.os.Message;
import
android.util.Log;
import
android.view.LayoutInflater;
import
android.view.View;
import
android.widget.ProgressBar;
import
android.widget.TextView;
import
android.widget.Toast;
public
class
DownFielToSdcard {
private
static
String SDPath;
ProgressBar pb;
TextView tv_percent;
int
downLoadFileSize, tatalsize;
Dialog dialog;
Context context;
public
DownFielToSdcard(Context context) {
super
();
this
.context = context;
SDPath = Environment.getExternalStorageDirectory() +
"/"
;
}
/**
* 在sdcard中创建文件
*
* @param fileName
* @return
* @throws Exception
*/
public
File CreateFile(String fileName)
throws
Exception {
File file =
new
File(SDPath + fileName);
file.createNewFile();
return
file;
}
/**
* 创建目录
*
* @param fileName
* @return
* @throws Exception
*/
public
File CreateFileSdDir(String dirName)
throws
Exception {
File sdDir =
new
File(SDPath + dirName);
sdDir.mkdir();
return
sdDir;
}
/**
* 判断文件是否存在
*
* @param fileName
* @return
*/
public
boolean
FileExist(String fileName) {
File file =
new
File(SDPath + fileName);
return
file.exists();
}
/**
* 思路:要下载文件,先得创建目录
*/
public
void
LoadToSdcard(
final
String strUrl,
final
String path,
final
String fileName)
throws
Exception {
if
(FileExist(
"test/missyou.mp3"
)) {
Toast.makeText(context, R.string.filehaved, Toast.LENGTH_LONG)
.show();
}
else
{
View view = LayoutInflater.from(context).inflate(
R.layout.download_dialog_xml,
null
);
pb = (ProgressBar) view.findViewById(R.id.down_pb);
tv_percent = (TextView) view.findViewById(R.id.pro_int);
dialog = AlertDialogUtil(view, context,
context.getString(R.string.waittingloading));
new
Thread(
new
Runnable() {
public
void
run() {
try
{
URL url =
new
URL(strUrl);
HttpURLConnection conection = (HttpURLConnection) url
.openConnection();
tatalsize = conection.getContentLength();
InputStream input = conection.getInputStream();
File file =
null
;
OutputStream outputstream =
null
;
CreateFileSdDir(path);
file = CreateFile(path + fileName);
outputstream =
new
FileOutputStream(file);
byte
data[] =
new
byte
[
1024
*
4
];
sentMassage(
0
);
while
(
true
) {
int
temp = input.read(data);
if
(temp == -
1
) {
break
;
}
outputstream.write(data,
0
, temp);
downLoadFileSize += temp;
sentMassage(
1
);
}
sentMassage(
2
);
outputstream.flush();
outputstream.close();
input.close();
}
catch
(Exception e) {
Toast.makeText(context, R.string.app_falls,
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
}).start();
}
}
/**
* 返回一个dialog
*
* @param view
* @param context
* @param string
* @return
*/
private
Dialog AlertDialogUtil(View view, Context context, String string) {
AlertDialog.Builder builder =
new
AlertDialog.Builder(context);
builder.setTitle(string);
builder.setIcon(R.drawable.icon);
builder.setView(view);
builder.create();
return
builder.show();
}
/**
* handler 处理动作
*/
Handler handler =
new
Handler() {
public
void
handleMessage(Message msg) {
super
.handleMessage(msg);
switch
(msg.what) {
case
0
:
pb.setMax(tatalsize);
break
;
case
1
:
pb.setProgress(downLoadFileSize);
int
result = downLoadFileSize *
100
/ tatalsize;
tv_percent.setText(context.getString(R.string.fileload)
+ result +
"%"
);
break
;
case
2
:
dialog.dismiss();
Toast.makeText(context, R.string.loagsucces, Toast.LENGTH_LONG)
.show();
Log.v(
"test"
,
"--->>> "
+
"end"
);
break
;
}
}
};
/**
*
* @param flag
* 消息类型
*/
public
void
sentMassage(
int
flag) {
Message msg =
new
Message();
msg.what = flag;
handler.sendMessage(msg);
}
}