Xutils实现视频上传显示带进度条

写了个xutils的上传带进度条。和大家分享下。希望给点建议。先上个图

Xutils实现视频上传显示带进度条_第1张图片Xutils实现视频上传显示带进度条_第2张图片

这里就只贴出重要的上传部分,其他的布局,按钮事件我想应该都很简单的吧! 这里就不贴出来了。有需要可留言。 -- --

先给俺定义的变量和Button,待会要用。

private Button sure_upload;//上传按钮
private EditText VieoName, briefContent;
//输入内容

private ImageView video_iv;//添加视频ImageView
private String path;//选择的视频路径
private String uploadUrl = Constants.UPLOADFILEVIDEO; // 上传服务器路径
private static final int USE_PICTURE = 0x101;//

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.uploadvideo_layout);
initView();
initTitle();
}

// 初始化
private void initView() {
VieoName = (EditText) findViewById(R.id.vieoName);
briefContent = (EditText) findViewById(R.id.brief_content);
sure_upload = (Button) findViewById(R.id.sure_upload);
sure_upload.setOnClickListener(this);
video_iv = (ImageView) findViewById(R.id.upload_video_imageiv);
video_iv.setOnClickListener(this);
}

@Override
public void onClick(View v) {
switch (v.getId()) {

case R.id.upload_video_imageiv:
Intent intent = new Intent();
intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(
Intent.createChooser(intent, "选择一个视频上传(建议在50M之间)"),
USE_PICTURE);
} catch (Exception e) {
e.printStackTrace();
}
break;

}

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data == null) {
Toast.makeText(UpLoadVideoActivity.this, "没有视频,请重新选择", 0).show();
return;
}
Uri uri = data.getData();
switch (requestCode) {
case USE_PICTURE:
if (uri == null) {
Toast.makeText(UpLoadVideoActivity.this, "视频出错,请重新选择", 0)
.show();
return;
}
try {
path = FileUtilities.getPath(this, uri);
} catch (URISyntaxException e) {
LogUtil.logD("result uri exception!");
}
if (!StringUtils.isEmpty(path)) {
video_iv.setImageBitmap(createBitmapThumbnail(path));
}
break;
}
}

//在这里就贴出了一个粗糙的Android自带的生成缩略图。

private Bitmap videoThumb;
// 生成缩略图
private Bitmap createBitmapThumbnail(String path2) {
videoThumb = ThumbnailUtils.createVideoThumbnail(path2,
Thumbnails.MINI_KIND);
return videoThumb;
}

最后贴出重要的部分,上传服务器(需要三个参数)

private ProgressDialog progressDialog;


// xutils上传static/upload/video/20160708100500264034.mp4
private void XutilsUploadVideo(String v_name, String desc)
throws IOException {
RequestParams params = new RequestParams("UTF-8");
params.addBodyParameter("userId", Constants.userid);
params.addBodyParameter("videoName", v_name);
params.addBodyParameter("description", desc);
params.addBodyParameter("file", new File(path), "video/
mpeg4");
HttpUtils http = new HttpUtils(1000 * 60);
http.configResponseTextCharset("UTF-8");
http.configCurrentHttpCacheExpiry(1000 * 60);
http.configSoTimeout(5000 * 60);
http.send(HttpRequest.HttpMethod.POST, uploadUrl, params,
new RequestCallBack() {
@Override
public void onStart() {
progressDialog = new ProgressDialog(
UpLoadVideoActivity.this);
progressDialog.setTitle("请稍等...");
progressDialog
.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setCancelable(true);
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.show();
}

计算百分比
进度 
@Override
public void onLoading(long total, long current,
boolean isUploading) {
if (isUploading) {
progressDialog
.setProgress((int) (((int) current / (float) total) * 100));
}
}


@Override
public void onSuccess(ResponseInfo responseInfo) {
progressDialog.dismiss();
LogUtil.logD(responseInfo.result + "");
Toast.makeText(UpLoadVideoActivity.this, "上传成功", 0)
.show();
finish();
}


@Override
public void onFailure(HttpException error, String msg) {
LogUtil.logD(error + "---" + msg.toString());
progressDialog.dismiss();
}
});
}
 

@Override
protected void onDestroy() {
super.onDestroy();
if (videoThumb != null) {
videoThumb.recycle();
}
}

上个图片

Xutils实现视频上传显示带进度条_第3张图片

暂且就这么多吧!里面我发现有个问题就是上传视频太大(100M)的话,会出现连接超时状态。暂时不知道咋怎么解决希望各位有方法的指点一二。感谢

部分源码地址 http://download.csdn.net/detail/ae_fring/9572650

你可能感兴趣的:(Xutils实现视频上传显示带进度条)