<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">最近在研究视频下载,在网上也查阅了许多资料,也下了几个demo进行看了,但是都有一些问题,后来无意间看到了xUtils框架,感觉对于下载问题简单了很多。</span>
xUtils框架可以从gitHub上进行下载:https://github.com/wyouflf/xUtils/
xUtils框架分为四大模块:DbUtils模块,ViewUtils模块,HttpUtils模块,BitmapUtils模块。对于这四个模块,gitHub官网上都做了很详细的说明,想要学习的人到上面网址上进行了解,而且还附有了demo以便于学习了解。我在这里就不多说了,我现在主要需要使用的是HttpUtils模块进行视频的下载,虽然xUtils框架也给了一个相关的demo,但是并不是我想要的,因此,我自己便写了一个相关的demo进行了解。
当然首先要使用xUtils的包,因此要从gitHub上下载xUtils框架所封装的包。
然后可以利用xUtils框架封装好的方法进行我所需要的视频下载:
public class MainActivity extends Activity implements OnClickListener {
private Button btn_down;
private ProgressBar download_pb;
private TextView tv;
private String sdPath = "/sdcard/xUtils/" + System.currentTimeMillis()
+ "lzfile.apk";
private String url = "http://apps.lidroid.com/apiv2/dl/0000000/com.lidroid.fileexplorer";
private HttpHandler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_down = (Button) findViewById(R.id.btn_down);
btn_down.setVisibility(View.VISIBLE);
download_pb = (ProgressBar) findViewById(R.id.download_pb);
tv = (TextView) findViewById(R.id.tv);
btn_down.setOnClickListener(this);
download_pb.setMax(100);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_down:
HttpUtils http = new HttpUtils();
handler = http.download(url, sdPath, true, false,
new RequestCallBack<File>() {
@SuppressWarnings("deprecation")
@Override
public void onStart() {
tv.setText("正在连接");
}
@Override
public void onLoading(long total, long current,
boolean isUploading) {
super.onLoading(total, current, isUploading);
btn_down.setText("正在下载");
download_pb.setProgress((int) ((double) current
/ (double) total * 100));
tv.setText((int) (current * 100 / total) + "%");
}
@Override
public void onSuccess(ResponseInfo<File> responseInfo) {
tv.setText(responseInfo.result.getPath());
}
@Override
public void onFailure(HttpException error, String msg) {
tv.setText(msg);
btn_down.setText("暂停<span style="font-family: Arial, Helvetica, sans-serif;">");</span>
}
});
break;
default:
break;
}
}
}