我正在尝试通过multiple videos
将API
上传到服务器,这类似于WhatsApp。
视频已成功上传。
从我的adapter
类上载时,我正在设置进度条。
但是,在我从服务器成功收到文件已上传的响应之前,progressBar
消失了。
我希望progressBar
在直到获得成功的Toast
消息之前都可见。
这是我用于上传视频媒体的Upload
方法:
private void uploadVideo(List<String> videoPathList) {
VideoUrl mVideoUrl = null;
String reqId = String.valueOf(dbHelper.getRequestId());
String userId = String.valueOf(dbHelper.getUserId());
for (int j = 0; j < videoPathList.size(); j++) {
String fullUrl = videoPathList.get(j);
int fullLength = fullUrl.length();
int lastIndex = fullUrl.lastIndexOf("/");
mVideoUrl = new VideoUrl();
mVideoUrl.setVideoUrl("/Uploads" + fullUrl.substring(lastIndex, fullLength));
// mVideoUrl.setVideoUrl(videoPathList.get(j));
//VideoUrl videoUrl = new VideoUrl("/Uploads/feedback_09132018023251PM.mp4");
videoUrlList.add(mVideoUrl);
}
mVideoAdapter.notifyDataSetChanged();
for (int i = 0; i < videoPathList.size(); i++) {
vidPath = videoPathList.get(i);
Log.d("Log", "filePath: " + vidPath);
SimpleMultiPartRequest smpr = new SimpleMultiPartRequest(Request.Method.POST, videoUploadUrl,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONArray jsonArray = new JSONArray(response);
for (int i = 0; i < jsonArray.length(); i++) {
String url = jsonArray.getJSONObject(i).getString("Url");
Log.d("Log", "response video: " + response);
Toast.makeText(getActivity(), url, Toast.LENGTH_SHORT).show();
}
mVideoAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("Log", "video response error: " + error);
Toast.makeText(getActivity(), "Uploading error", Toast.LENGTH_SHORT).show();
}
});
smpr.addStringParam("Userid", userId);
smpr.addStringParam("RequestId", String.valueOf(2));
smpr.addStringParam("ContentType", "2");
smpr.addFile("File", vidPath);
Log.d("Log", "filePath smpr: " + vidPath);
RequestQueue mRequestQue = Volley.newRequestQueue(getActivity());
mRequestQue.getCache().invalidate(videoUploadUrl, true);
mRequestQue.add(smpr);
}
}
这是我的
Adapter
类:@Override
public void onBindViewHolder(final videoAdapter.videoViewHolder holder, final int position) {
if (getItemViewType(position) == TYPE_LAYOUT_UPLOAD){
uploadViewHolder upHolder = (uploadViewHolder) holder;
upHolder.uploadVideo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
uploadVideoActivity.onUploadActivity();
}
});
}else {
final VideoUrl videoUrlList = videoList.get(position-1);
String videoPath = videoUrlList.getVideoUrl();
int userType = videoUrlList.getUserType();
try {
//bMap = retriveVideoFrameFromVideo("http://abcco.ao"+videoPath);
bMap = retriveVideoFrameFromVideo(videoPath);
} catch (Throwable throwable) {
throwable.printStackTrace();
}
if (userType == 1){
holder.removeItem.setVisibility(View.INVISIBLE);
}else {
holder.removeItem.setVisibility(View.VISIBLE);
}
Glide.with(mContext)
.load("http://abc.vcm"+videoPath)
.thumbnail(Glide.with(mContext).load("http://abc.vcm"+videoPath))
.listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target,
boolean isFirstResource) {
holder.pBar.setVisibility(View.GONE);
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource,
boolean isFirstResource) {
holder.pBar.setVisibility(View.GONE);
return false;
}
})
.into(holder.videoThumb);
holder.videoThumb.setOnClickListener(new videoClickListener(position));
holder.removeItem.setOnClickListener(new removeItemClickListener(position));
}
}
这是该过程的示例屏幕截图。显示了进度条,但是在我从服务器获得完整响应之前消失了。
最佳答案
最好您可以在uploadVideo()方法内部使用进度条,例如,
内部onResponse()方法
Log.d("Log", "response video: " + response);
Toast.makeText(getActivity(), url, Toast.LENGTH_SHORT).show();
pBar.setVisibility(View.GONE);
内部Error()方法
Log.d("Log", "video response error: " + error);
Toast.makeText(getActivity(), "Uploading error", Toast.LENGTH_SHORT).show();
pBar.setVisibility(View.GONE);
为此,您需要初始化进度条并通过参数传递。
祝一切顺利..