private void onUploadProgress(UploadProgress progress) { float fraction = progress.getFraction(); int percent = Math.round(fraction * 100); if (percent >= 100) { progressPercent.setText(getString(R.string.uploading)); progressBar.setIndeterminate(true); } else { String humanReadablePerSecond = getHumanReadablePerSecond(progress.getBytesPerSecond()); progressBar.setIndeterminate(false); progressPercent.setText(getString(R.string.percent, percent)); uploadRate.setText(humanReadablePerSecond); progressBar.setProgress(percent); } }
/** * 上传文件 :返回值为0表示上传失败,返回值为1表示上传成功 * * @param dataInfo * @return * @throws Exception */ public int upload( UploadDataInfo dataInfo, VideoItem video, UploadProgress uploadProgressInterface) throws Exception { // 上传成之后返回的值 int result = CRMConstant.UPLOAD_RESULT_FAILURE; if (dataInfo != null) { // 上传文件的fileid String fileid = dataInfo.getFileID(); // 上传路径 String urlPath = dataInfo.getUploadURL(); // 需要上传的文件 File videoFile = new File(video.getPath()); if (videoFile.exists()) { // 通过第一步取得的参数,开始构造上传的 参数 inputStream = new FileInputStream(videoFile); } try { // 构造HTTP协议的参数 Map<String, String> params = new HashMap<String, String>(); params.put("fileid", fileid); // 循环标志 boolean runFlag = false; // 缓存大小 byte[] bufferInpuStream = new byte[BUFFER_SIZE]; // 一次上传的文件大小 int bufferLength = 0; // 已经上传的文件大小 int uploadSize = 0; String strResult = ""; do { // 读取文件 if ((bufferLength = inputStream.read(bufferInpuStream)) != -1) { // 每次都要重新构造上传文件的描述,包括上传的起始位置,和上传的文件大小 params.put("length", String.valueOf(bufferLength)); params.put("start", String.valueOf(uploadSize)); uploadSize += bufferLength; // 上传文件,并返回上传之后服务器的反馈 byte[] byteResult = httpConnection.updataDataByPost( urlPath, params, bufferInpuStream, bufferLength, video.getTitle()); Log.d(TAG, "---------正在上传文件------------------>"); Log.d(TAG, "已上传文件大小:" + uploadSize); // 通过json解析,得到服务器的反馈值 strResult = jsonHandler.handlerUploadResult(new String(byteResult)); Log.d(TAG, "上传进度:" + strResult); if (uploadProgressInterface != null) { uploadProgressInterface.updateUploadProgress(strResult); } if (!CRMConstant.UPLOAD_FAILURE.equals(strResult)) { runFlag = true; } else { runFlag = false; // 如果中途传输失败,则返回 return CRMConstant.UPLOAD_RESULT_FAILURE; } } else { runFlag = false; Log.d(TAG, "数据已经读完"); } } while (runFlag); if (Double.parseDouble(strResult) >= 100) { result = CRMConstant.UPLOAD_RESULT_SUCCESS; } } catch (Exception e) { e.printStackTrace(); } } return result; }
void runTask() { ArrayList<Picture> pictures = g.getAllUploadablePictures(); // get total file size long totalSize = 0; Iterator<Picture> iter = pictures.iterator(); while (iter.hasNext()) { Picture p = iter.next(); totalSize += p.getFileSize(); } // transferListener = new MyTransferListener(su); // transferListener.sizeAllFiles = totalSize; // transferListener.numberAllFiles = pictures.size(); su.startProgress(StatusUpdate.LEVEL_UPLOAD_ALL, 0, 100, GRI18n.getString(MODULE, "upPic"), false); if (su instanceof UploadProgress) { ((UploadProgress) su).setCancelListener(new ActionListener() { public void actionPerformed(ActionEvent e) { su.updateProgressStatus(StatusUpdate.LEVEL_UPLOAD_ALL, GRI18n.getString(MODULE, "upStop")); su.setUndetermined(StatusUpdate.LEVEL_UPLOAD_ALL, true); interrupt(); long startTime = System.currentTimeMillis(); while (!terminated && System.currentTimeMillis() < startTime + 10000) { try { Thread.sleep(1000); } catch (InterruptedException e1) { } } if (!terminated) { Log.log(Log.LEVEL_ERROR, "Thread would not terminate properly: killing it"); thread.stop(); // since we killed the thread, it's not going to clean up after itself cleanUp(); } ((UploadProgress) su).done(); } }); } // upload each file, one at a time boolean allGood = true; //int uploadedCount = 0; iter = pictures.iterator(); while (iter.hasNext() /*&& allGood*/ && !interrupt) { Picture p = iter.next(); /*Object[] params = { p.toString(), new Integer((uploadedCount + 1)), new Integer(pictures.size()), new Integer((int) ((transferListener.sizeFilesDone + transferListener.sizeThisFile) / 1024 / 1024)), new Integer((int) (transferListener.sizeAllFiles / 1024 / 1024)), transferListener.getProjectedTimeLeft() }; su.updateProgressStatus(StatusUpdate.LEVEL_UPLOAD_ALL, GRI18n.getString(MODULE, "upStatus", params));*/ allGood = uploadPicture(p); //su.updateProgressValue(StatusUpdate.LEVEL_UPLOAD_ALL, ++uploadedCount); if (allGood) { p.getParentAlbum().removePicture(p); } } if (allGood) { su.stopProgress(StatusUpdate.LEVEL_UPLOAD_ALL, GRI18n.getString(MODULE, "upComplete")); if (su instanceof UploadProgress) { if (((UploadProgress) su).isShutdown()) { GalleryRemote._().getCore().shutdown(true); } } g.setDirty(false); GalleryRemote._().getCore().flushMemory(); } else { su.stopProgress(StatusUpdate.LEVEL_UPLOAD_ALL, GRI18n.getString(MODULE, "upFailed")); } }