最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android實現(xiàn)斷點續(xù)傳功能

 更新時間:2022年07月27日 09:44:49   作者:wy15230527528  
這篇文章主要為大家詳細介紹了Android實現(xiàn)斷點續(xù)傳功能,能在上次的斷點處繼續(xù)上傳,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android實現(xiàn)斷點續(xù)傳的具體代碼,供大家參考,具體內(nèi)容如下

斷點續(xù)傳功能,在文件上傳中斷時,下次上傳同一文件時,能在上次的斷點處繼續(xù)上傳,可節(jié)省時間和流量

總結(jié)規(guī)劃步驟:

1.給大文件分片,每一個大文件發(fā)送前,相對應(yīng)的創(chuàng)建一個文件夾存放所有分片

2.上傳接口,每一個分片上傳完成就刪掉,直到所有分片上傳完成,再刪掉存放分片的文件夾,服務(wù)器把分片合成完整的文件。

先看分片功能,傳入的3個參數(shù)分別為源文件地址,分片大小,存放分片的文件夾地址。返回的是分片個數(shù)。

/**
? ? ?*
? ? ?* @param sourceFilePath ? ?源文件地址
? ? ?* @param partFileLength ? ?分割文件的每一個片段大小標準
? ? ?* @param splitPath ? ? ? ? 分割之后片段所在文件夾
? ? ?* @return
? ? ?* @throws Exception
? ? ?*/
? ? public static int splitFile(String sourceFilePath, int partFileLength, String splitPath) throws Exception {
? ? ? ? File sourceFile = null;
? ? ? ? File targetFile = null;
? ? ? ? InputStream ips = null;
? ? ? ? OutputStream ops = null;
? ? ? ? OutputStream configOps = null;//該文件流用于存儲文件分割后的相關(guān)信息,包括分割后的每個子文件的編號和路徑,以及未分割前文件名
? ? ? ? Properties partInfo = null;//properties用于存儲文件分割的信息
? ? ? ? byte[] buffer = null;
? ? ? ? int partNumber = 1;
? ? ? ? sourceFile = new File(sourceFilePath);//待分割文件
? ? ? ? ips = new FileInputStream(sourceFile);//找到讀取源文件并獲取輸入流
? ? ? ? //創(chuàng)建一個存放分片的文件夾
? ? ? ? File tempFile = new File(splitPath);
? ? ? ? if (!tempFile.exists()) {
? ? ? ? ? ? tempFile.mkdirs();
? ? ? ? }
? ? ? ? configOps = new FileOutputStream(new File(tempFile.getAbsolutePath() + File.separator + "config.properties"));
? ? ? ? buffer = new byte[partFileLength];//開辟緩存空間
? ? ? ? int tempLength = 0;
? ? ? ? partInfo = new Properties();//key:1開始自動編號 value:文件路徑
?
? ? ? ? int sliceCount = 0;
? ? ? ? while ((tempLength = ips.read(buffer, 0, partFileLength)) != -1) {
? ? ? ? ? ? String targetFilePath = tempFile.getAbsolutePath() + File.separator + "part_" + (partNumber);//分割后的文件路徑+文件名
? ? ? ? ? ? sliceCount = partNumber;
? ? ? ? ? ? partInfo.setProperty((partNumber++) + "", targetFilePath);//將相關(guān)信息存儲進properties
? ? ? ? ? ? targetFile = new File(targetFilePath);
? ? ? ? ? ? ops = new FileOutputStream(targetFile);//分割后文件
? ? ? ? ? ? ops.write(buffer, 0, tempLength);//將信息寫入碎片文件
?
? ? ? ? ? ? ops.close();//關(guān)閉碎片文件
? ? ? ? }
? ? ? ? partInfo.setProperty("name", sourceFile.getName());//存儲源文件名
? ? ? ? partInfo.setProperty("sliceCount", sliceCount + "");//存儲分片個數(shù)
? ? ? ? partInfo.store(configOps, "ConfigFile");//將properties存儲進實體文件中
? ? ? ? ips.close();//關(guān)閉源文件流
?
? ? ? ? return sliceCount;
? ? }

接下來,和服務(wù)器協(xié)商接口,一共2個接口

1.uploadLargeFilePre,傳入?yún)?shù)除了常規(guī)上傳文件參數(shù)之外加了分片個數(shù)sliceCount和fileHashcode。這一步是給服務(wù)器開辟存放分片的控件的,不執(zhí)行上傳操作,文件哈希值作為識別分片歸屬的唯一標準。返回int類型rCode,根據(jù)rCode結(jié)果判斷是否執(zhí)行第2步上傳動作

2.uploadLargeFile,傳入?yún)?shù)除了常規(guī)上傳文件參數(shù)之外加了分片path,分片index,isFinished。isFinished是最后一片的依據(jù)??蛻舳司驮谏蟼鹘涌诘膔esponse里繼續(xù)上傳下一個分片,直到最后一片上傳完成,服務(wù)器才會返給這個大文件的url。

看代碼

protected void addFileMsg(String path) {
? ? ? ? forceLogin();
?
? ? ? ? if (path == null) {
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? File file = new File(path);
? ? ? ? if (file.exists()) {
? ? ? ? ? ? ImMsg msg = new ImMsg();
? ? ? ? ? ? msg.setType(ImMsg.MSG_TYPE_FILE);
? ? ? ? ? ? msg.setTime(System.currentTimeMillis());
? ? ? ? ? ? msg.setMsgReaded();
? ? ? ? ? ? msg.setReceiveTime(System.currentTimeMillis());
? ? ? ? ? ? AccountInfo accountInfo = IMApp.instance().getAccountInfo();
? ? ? ? ? ? msg.setUserId(accountInfo.getUserId());
? ? ? ? ? ? msg.setSex(accountInfo.getSex());
? ? ? ? ? ? msg.setUserName(accountInfo.mNickName);
? ? ? ? ? ? msg.setHeadUrl(accountInfo.mHead);
? ? ? ? ? ? msg.mMasterId = mMasterId;
? ? ? ? ? ? msg.setMsg(new File(path).getName());
? ? ? ? ? ? msg.setPicPath(path);
? ? ? ? ? ? msg.setState(ImMsg.STATE_SEND_UPLOADING);
? ? ? ? ? ? String ext = null;
? ? ? ? ? ? if (path.endsWith("doc") || path.endsWith("docx")) {
? ? ? ? ? ? ? ? ext = "doc";
? ? ? ? ? ? } else if (path.endsWith("xls") || path.endsWith("xlsx")) {
? ? ? ? ? ? ? ? ext = "xls";
? ? ? ? ? ? } else if (path.endsWith("ppt") || path.endsWith("pptx")) {
? ? ? ? ? ? ? ? ext = "ppt";
? ? ? ? ? ? } else if (path.endsWith("pdf")) {
? ? ? ? ? ? ? ? ext = "pdf";
? ? ? ? ? ? }
? ? ? ? ? ? msg.setMsg_extend3(ext);
?
? ? ? ? ? ? msg.mFileSize = (int) new File(path).length();
? ? ? ? ? ? msg.setHasAtt(hasAtt());
? ? ? ? ? ? addMsgToDB(msg);
?
? ? ? ? ? ? isFileListSingle = fileList.size() == 1;
? ? ? ? ? ? SPHelper spHelper = SPHelper.build();
? ? ? ? ? ? int lastSlices = spHelper.get(slice_old, 0);
? ? ? ? ? ? String aesPath = spHelper.get(slice_aesEncPath, "");
? ? ? ? ? ? String lastPath = spHelper.get(slice_picPath, "");
? ? ? ? ? ? int tempTotalCount = spHelper.get(CommonUtils.FAILED_TEMP_SEND_TOTAL_COUNT, 0);//實際發(fā)送總個數(shù)
? ? ? ? ? ? if (lastSlices == 1 && tempTotalCount > 1) {
? ? ? ? ? ? ? ? /**
? ? ? ? ? ? ? ? ?* 讀取上次失敗文件的進度,如果還剩一個失敗文件,但是上次發(fā)送的總文件個數(shù)大于1,
? ? ? ? ? ? ? ? ?* 則fileList.size() == 1這個判斷不能用,在所有判斷處加一個標志
? ? ? ? ? ? ? ? ?*/
? ? ? ? ? ? ? ? isFileListSingle = false;
? ? ? ? ? ? }
? ? ? ? ? ? //根據(jù)文件大小,判斷是否用分片上傳 modify hexiaokang 2019年11月5日11點01分
? ? ? ? ? ? if (new File(path).length() < Global.FILE_SPILT_LENGTH) {//文件小于5M,常規(guī)上傳方式
? ? ? ? ? ? ? ? upLoadFile(msg);
? ? ? ? ? ? } else {
?
? ? ? ? ? ? ? ? File sourceFile = new File(path);
? ? ? ? ? ? ? ? //分片所在文件夾,以未做AES加密的源文件名作為 分片文件夾名字
? ? ? ? ? ? ? ? String sliceDir = sourceFile.getParent() + File.separator + sourceFile.getName() + "_split";
?
? ? ? ? ? ? ? ? if (lastSlices == 1
? ? ? ? ? ? ? ? ? ? ? ? && path.equals(lastPath)
? ? ? ? ? ? ? ? ? ? ? ? && new File(sliceDir).exists()
? ? ? ? ? ? ? ? ? ? ? ? && !aesPath.equals("")
? ? ? ? ? ? ? ? ? ? ? ? && new File(aesPath).exists()) {//發(fā)送上次的舊文件中斷的分片
? ? ? ? ? ? ? ? ? ? //只走一次
? ? ? ? ? ? ? ? ? ? spHelper.put(slice_old, 0);
? ? ? ? ? ? ? ? ? ? sliceIndex = spHelper.get(slice_sliceIndex, 0);
? ? ? ? ? ? ? ? ? ? int count = spHelper.get(slice_sliceCount, 0);
? ? ? ? ? ? ? ? ? ? LogUtil2.i(TAG + "&onUploadComplete", "sendOldFiles sliceIndex = " + sliceIndex + ", sliceCount = " + count);
? ? ? ? ? ? ? ? ? ? largeFilePre = true;
? ? ? ? ? ? ? ? ? ? upLoadLargeFilePre(msg, count, sliceDir, aesPath);
? ? ? ? ? ? ? ? } else {//發(fā)送大文件正常流程
? ? ? ? ? ? ? ? ? ? //給文件分片
? ? ? ? ? ? ? ? ? ? int partFileLength = Global.FILE_SPILT_LENGTH;//指定分割的子文件大小為5M
? ? ? ? ? ? ? ? ? ? //先對文件做AES加密,再進行分片,這里很重要
? ? ? ? ? ? ? ? ? ? String aesEncPath = EncryptMgr.encFile(path, accountInfo.getEncTime(), accountInfo.getEncKey());
? ? ? ? ? ? ? ? ? ? File f = new File(aesEncPath);
? ? ? ? ? ? ? ? ? ? LogUtil2.i(TAG + "&onUploadComplete", "ChatMsgActivity.addFileMsg: big file enc path=" + path);
?
? ? ? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? ? ? sliceCount = FileSliceUtil.splitFile(aesEncPath, partFileLength, sliceDir);//將文件分割
? ? ? ? ? ? ? ? ? ? } catch (Exception e) {
? ? ? ? ? ? ? ? ? ? ? ? LogUtil2.e(TAG, "split.e:" + e.getMessage());
? ? ? ? ? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? ? ? ? ? }
// ? ? ? ? ? ? ? ? ?LogUtil2.e(TAG+"&onUploadComplete", "ChatMsgActivity.addFileMsg: sliceCount:" + sliceCount);
?
? ? ? ? ? ? ? ? ? ? //分片上傳
? ? ? ? ? ? ? ? ? ? largeFilePre = false;
? ? ? ? ? ? ? ? ? ? upLoadLargeFilePre(msg, sliceCount, sliceDir, aesEncPath);
?
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }

上面這一塊代碼,除了小文件常規(guī)上傳,就是大文件上傳方式了。大文件上傳這里分了2種情況,

1.發(fā)送上次中斷的大文件分片,這里就不用分片了,直接從sp拿到上次中斷的分片count、index等信息直接上傳

2.從分片到發(fā)送的全部流程,因為我這里對文件做了AES加密,所以是加密之后再分片

接下來就是上傳過程

public void upLoadLargeFilePre(final ImMsg msg, final int sliceCount, final String sliceDir, final String aesEncPath) {
? ? ? ? if (msg.getState() != ImMsg.STATE_SEND_WAITING) {
? ? ? ? ? ? msg.setState(ImMsg.STATE_SEND_UPLOADING);
? ? ? ? ? ? mListAdapter.notifyDataSetChanged();
? ? ? ? }
? ? ? ? AccountInfo accountInfo = IMApp.instance().getAccountInfo();
? ? ? ? UploadFileHelper helper = new UploadFileHelper(accountInfo.getEncTime(), accountInfo.getEncKey(),
? ? ? ? ? ? ? ? new UploadFileCallback() {
?
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onError(Call call, Exception e) {
? ? ? ? ? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? ? ? ? ? ? ? LogUtil2.e("onUploadComplete", "ChatMsgActivity.onError: error(split_upload):" + e.toString()+", call = "+call);
? ? ? ? ? ? ? ? ? ? ? ? failCount++;
// ? ? ? ? ? ? ? ? ? ? ? ? ? ?if (msg.getPicPath() != null && msg.getPicPath().contains(SDCardUtil.getSDcardPathEx())) {
// ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?new File(msg.getPicPath()).delete();
// ? ? ? ? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? ? ? ? ? btn_other_sendfile.setClickable(true);
? ? ? ? ? ? ? ? ? ? ? ? CommonUtils.isSendBtnClickable = true;
? ? ? ? ? ? ? ? ? ? ? ? Intent comIntent = new Intent();
? ? ? ? ? ? ? ? ? ? ? ? comIntent.setAction(CommonUtils.USBFILE_COMPLETE_SEND);
? ? ? ? ? ? ? ? ? ? ? ? comIntent.putExtra("fail_count", failCount);
? ? ? ? ? ? ? ? ? ? ? ? sendBroadcast(comIntent);
?
? ? ? ? ? ? ? ? ? ? ? ? tempProgress = 0;
? ? ? ? ? ? ? ? ? ? ? ? largeFilePre = false;
? ? ? ? ? ? ? ? ? ? ? ? //todo 上傳失敗,上傳任務(wù)終止 (需要保存失敗msg的信息,以及分片信息)
? ? ? ? ? ? ? ? ? ? ? ? String picPath = msg.getPicPath();
? ? ? ? ? ? ? ? ? ? ? ? // 保存picPath, sliceIndex, sliceCount, aesEncPath
? ? ? ? ? ? ? ? ? ? ? ? SPHelper spHelper = SPHelper.build();
? ? ? ? ? ? ? ? ? ? ? ? spHelper.put(slice_picPath, picPath);
? ? ? ? ? ? ? ? ? ? ? ? spHelper.put(slice_sliceIndex, sliceIndex);
? ? ? ? ? ? ? ? ? ? ? ? spHelper.put(slice_sliceCount, sliceCount);
? ? ? ? ? ? ? ? ? ? ? ? spHelper.put(slice_aesEncPath, aesEncPath);
? ? ? ? ? ? ? ? ? ? ? ? spHelper.put(slice_old, 1);//標記,1表示有上次失敗的碎片,處理完上次失敗的碎片之后設(shè)置為0
? ? ? ? ? ? ? ? ? ? ? ? return;
? ? ? ? ? ? ? ? ? ? }
?
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onResponse(UploadFileAckPacket uploadFileAckPacket) {
? ? ? ? ? ? ? ? ? ? ? ? LogUtil2.i("onUploadComplete", "ChatMsgActivity.onResponse: pre upload ack packet code="+uploadFileAckPacket.getRetCode()+", desc="+uploadFileAckPacket.getRetDesc());
? ? ? ? ? ? ? ? ? ? ? ? if (getMsgFromDB(msg.getId()) == null) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? msg.setState(ImMsg.STATE_SEND_FAILED);
? ? ? ? ? ? ? ? ? ? ? ? ? ? LogUtil2.i("onUploadComplete", "msg not exist d = (split_upload)" + msg.getId());
? ? ? ? ? ? ? ? ? ? ? ? ? ? updateMsgToDB(msg);
? ? ? ? ? ? ? ? ? ? ? ? ? ? mListAdapter.notifyDataSetChanged();
?
? ? ? ? ? ? ? ? ? ? ? ? ? ? failCount++;
? ? ? ? ? ? ? ? ? ? ? ? ? ? btn_other_sendfile.setClickable(true);
? ? ? ? ? ? ? ? ? ? ? ? ? ? CommonUtils.isSendBtnClickable = true;
? ? ? ? ? ? ? ? ? ? ? ? ? ? Intent comIntent = new Intent();
? ? ? ? ? ? ? ? ? ? ? ? ? ? comIntent.setAction(CommonUtils.USBFILE_COMPLETE_SEND);
? ? ? ? ? ? ? ? ? ? ? ? ? ? sendBroadcast(comIntent);
?
? ? ? ? ? ? ? ? ? ? ? ? ? ? tempProgress = 0;
? ? ? ? ? ? ? ? ? ? ? ? ? ? return;
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? if (uploadFileAckPacket != null && uploadFileAckPacket.isSuccess()) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? LogUtil2.i("onUploadComplete", "msg exist and sucess(uploadFileAckPacket.sliceIndex(split_upload)):" + uploadFileAckPacket.sliceIndex+", sliceCount="+sliceCount+", url="+uploadFileAckPacket.mUrl);
? ? ? ? ? ? ? ? ? ? ? ? ? ? if (sliceIndex < sliceCount && TextUtils.isEmpty(uploadFileAckPacket.mUrl)) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //更新進度條
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (isFileListSingle) {//單個大文件
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? int pro = 100 * sliceIndex / sliceCount;
?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Intent uploadIntent = new Intent();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? uploadIntent.setAction(CommonUtils.USBFILE_PROGRESS_SEND);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? uploadIntent.putExtra("sentCount", -1);//-1 代表發(fā)送的是單個文件
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? uploadIntent.putExtra("sentPro", pro);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? sendBroadcast(uploadIntent);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } else {//一次發(fā)送多個文件,包括大文件
?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //刪除掉上傳成功的分片
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? String slicePath = sliceDir + File.separator + "part_" + (sliceIndex);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? new File(slicePath).delete();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? sliceIndex++;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //還有待上傳的分片
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? upLoadLargeFilePre(msg, sliceCount, sliceDir, aesEncPath);
? ? ? ? ? ? ? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //所有分片上傳完成
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? largeFilePre = false;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? LogUtil2.i("onUploadComplete", "ChatMsgActivity.onResponse: 所有分片上傳完成 largeFilePre="+largeFilePre);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? msg.updateImageUpLoadProgress(100);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? msg.setPicBigUrl(uploadFileAckPacket.mUrl);
?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //這里刪除原文件
?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (msg.getPicPath() != null && msg.getPicPath().contains(SDCardUtil.getSDcardPathEx())) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? File file = new File(msg.getPicPath());
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //刪除分片所在的文件夾
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? FileUtil.deleteFolderFile(sliceDir, true);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //刪除文件
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? file.delete();
// ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?EventBus.getDefault().post(new EncFileEvent(EncFileEvent.COM_FILE_DELETE, msg.getPicPath(), 0));
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? msg.setPicPath("");
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (msg.getType() == ImMsg.MSG_TYPE_IMAGE) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? msg.setPicSmallUrl(uploadFileAckPacket.mThumbnail);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? msg.mThumbWidth = this.mThumbWidth;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? msg.mThumbHeight = this.mThumbHeight;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
?
?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? LogUtil2.i("onUploadComplete", "msg exist and sucess(msg.getPicBigUrl()(split_upload)):" + msg.getPicBigUrl());
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? sendMsg(msg);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? sentCount++;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? sliceIndex = 1;
?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (isFileListSingle) {//單個文件不用處理
?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //傳遞上傳進度
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Intent uploadIntent = new Intent();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? uploadIntent.setAction(CommonUtils.USBFILE_PROGRESS_SEND);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? uploadIntent.putExtra("sentCount", sentCount);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? sendBroadcast(uploadIntent);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //繼續(xù)上傳下一個文件
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (sentCount < fileList.size()) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? addFileMsg(fileList.get(sentCount).getAbsolutePath());
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //所有文件上傳完成
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? btn_other_sendfile.setClickable(true);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? CommonUtils.isSendBtnClickable = true;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Intent comIntent = new Intent();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? comIntent.setAction(CommonUtils.USBFILE_COMPLETE_SEND);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? sendBroadcast(comIntent);
?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? tempProgress = 0;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? ? ? ? ? LogUtil2.e("onUploadComplete", "rCode(split_upload)):" + uploadFileAckPacket.getRetCode()+", sliceIndex="+uploadFileAckPacket.sliceIndex);
? ? ? ? ? ? ? ? ? ? ? ? ? ? if(uploadFileAckPacket.getRetCode()==1343688774){
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? LogUtil2.e("onUploadComplete", "ChatMsgActivity.onResponse: 缺片了!");
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? msg.setState(ImMsg.STATE_SEND_FAILED);
? ? ? ? ? ? ? ? ? ? ? ? ? ? updateMsgToDB(msg);
? ? ? ? ? ? ? ? ? ? ? ? ? ? if (!IMMsgMgr.notifyActivity(IMMsgMgr.IM_CMD_IMP2PMSG_ACK, msg.getUserId(), msg.hasAtt(), false, msg)) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? mListAdapter.notifyDataSetChanged();
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
?
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void inProgress(float progress) {
? ? ? ? ? ? ? ? ? ? ? ? super.inProgress(progress);
// ? ? ? ? ? ? ? ? ? ? ? ?LogUtil2.i("onUploadProgress", "inProgress " + progress+", path="+msg.getPicPath()+", sliceDir="+sliceDir);
? ? ? ? ? ? ? ? ? ? ? ? LogUtil2.i("onUploadProgress", "ChatMsgActivity.inProgress: sliceCount="+sliceCount+", sliceIndex="+sliceIndex+", sentCount="+sentCount+", list.size="+fileList.size()+", progress="+progress+", tempProgress="+tempProgress);
? ? ? ? ? ? ? ? ? ? ? ? int pro = (int) (progress * 100);
? ? ? ? ? ? ? ? ? ? ? ? if (new File(msg.getPicPath()).length() < Global.FILE_SPILT_LENGTH) {
//
? ? ? ? ? ? ? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? ? ? ? ? ? ? int allPro= (int)(100*(progress+(sliceIndex-1))/sliceCount);
? ? ? ? ? ? ? ? ? ? ? ? ? ? if (isFileListSingle && allPro - tempProgress >= 1) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //todo 分片上傳,進度條重新計算,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //多個文件上傳不用考慮,這里重新計算單個文件的上傳問題
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? LogUtil2.i("onUploadProgress", "ChatMsgActivity.inProgress: big file allPro="+allPro);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Intent uploadIntent = new Intent();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? uploadIntent.setAction(CommonUtils.USBFILE_PROGRESS_SEND);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? uploadIntent.putExtra("sentCount", -1);//-1代表發(fā)送的是單個文件,這里是針對用戶感知而言的代碼邏輯
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? uploadIntent.putExtra("sentPro", allPro);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? tempProgress = allPro;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? sendBroadcast(uploadIntent);
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? // 更新
? ? ? ? ? ? ? ? ? ? ? ? mListAdapter.notifyDataSetChanged();
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }, msg);
? ? ? ? LogUtil2.i("onUploadComplete", "ChatMsgActivity.upLoadLargeFilePre: largeFilePre="+largeFilePre);
? ? ? ? if (largeFilePre) {//todo 是否有過預(yù)處理
? ? ? ? ? ? String slicePath = sliceDir + File.separator + "part_" + (sliceIndex);
? ? ? ? ? ? int isFinished = sliceIndex == sliceCount ? 1 : 0;
? ? ? ? ? ? helper.upLoadLargeFile(aesEncPath, slicePath, sliceIndex, isFinished);
? ? ? ? } else {
? ? ? ? ? ? //上傳大文件 預(yù)處理
? ? ? ? ? ? int rCode = helper.upLoadLargeFilePre(aesEncPath, sliceCount);
? ? ? ? ? ? if (rCode == 0) {//預(yù)處理成功,可以執(zhí)行上傳任務(wù)
? ? ? ? ? ? ? ? largeFilePre = true;
? ? ? ? ? ? ? ? String slicePath = sliceDir + File.separator + "part_" + (sliceIndex);
? ? ? ? ? ? ? ? int isFinished = sliceIndex == sliceCount ? 1 : 0;
? ? ? ? ? ? ? ? helper.upLoadLargeFile(aesEncPath, slicePath, sliceIndex, isFinished);
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? //預(yù)處理失敗,不執(zhí)行上傳任務(wù)
? ? ? ? ? ? ? ? LogUtil2.i("onUploadComplete", "somewordsrCode:" + rCode+", before plus faileCount="+failCount);
?
? ? ? ? ? ? ? ? msg.setState(ImMsg.STATE_SEND_FAILED);
? ? ? ? ? ? ? ? updateMsgToDB(msg);
? ? ? ? ? ? ? ? mListAdapter.notifyDataSetChanged();
? ? ? ? ? ? ? ? failCount++;
? ? ? ? ? ? ? ? btn_other_sendfile.setClickable(true);
? ? ? ? ? ? ? ? CommonUtils.isSendBtnClickable = true;
? ? ? ? ? ? ? ? Intent comIntent = new Intent();
? ? ? ? ? ? ? ? comIntent.putExtra("upload_error", 0);
? ? ? ? ? ? ? ? comIntent.putExtra("fail_count",failCount);
? ? ? ? ? ? ? ? comIntent.setAction(CommonUtils.USBFILE_COMPLETE_SEND);
? ? ? ? ? ? ? ? LogUtil.LogShow("onUploadComplete", "ChatMsgActivity.upLoadLargeFilePre: before sendIntent failCount="+failCount);
? ? ? ? ? ? ? ? sendBroadcast(comIntent);
?
? ? ? ? ? ? ? ? failCount=0;
? ? ? ? ? ? ? ? tempProgress = 0;
? ? ? ? ? ? }
? ? ? ? }
? ? }

上面這一塊就是上傳過程

int rCode = helper.upLoadLargeFilePre(aesEncPath, sliceCount); 這一句為第一個接口預(yù)處理,根據(jù)結(jié)果決定是否執(zhí)行上傳。

helper.upLoadLargeFile(aesEncPath, slicePath, sliceIndex, isFinished); 這一句執(zhí)行上傳,在onResponse里處理上傳結(jié)果,我這里有多個文件連續(xù)上傳,所以結(jié)果處理看起來有點凌亂。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android ScrollView取消慣性滾動的方法

    Android ScrollView取消慣性滾動的方法

    下面小編就為大家?guī)硪黄狝ndroid ScrollView取消慣性滾動的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • Android  ADB詳細介紹及用法

    Android ADB詳細介紹及用法

    本文主要介紹Android ADB,這里整理了Android ADB的文檔資料,詳細介紹了adb 命令,有需要的小伙伴可以參考下
    2016-08-08
  • android實現(xiàn)文件讀寫功能

    android實現(xiàn)文件讀寫功能

    這篇文章主要為大家詳細介紹了android實現(xiàn)文件讀寫功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-06-06
  • Android訪問php取回json數(shù)據(jù)實例

    Android訪問php取回json數(shù)據(jù)實例

    Android訪問php取回json數(shù)據(jù),實現(xiàn)代碼如下,遇到訪問網(wǎng)絡(luò)的權(quán)限不足在AndroidManifest.xml中,需要進行如下配置
    2013-06-06
  • Android自定義View繪制彩色圓弧

    Android自定義View繪制彩色圓弧

    這篇文章主要為大家詳細介紹了Android自定義View繪制彩色圓弧,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • Android利用貝塞爾曲線繪制動畫的示例代碼

    Android利用貝塞爾曲線繪制動畫的示例代碼

    本篇就借由動畫驅(qū)動貝塞爾曲線繪制看看動起來的貝塞爾曲線什么效果。文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2022-05-05
  • android 調(diào)用系統(tǒng)的照相機和圖庫實例詳解

    android 調(diào)用系統(tǒng)的照相機和圖庫實例詳解

    android手機有自帶的照相機和圖庫,我們做的項目中有時用到上傳圖片到服務(wù)器,今天做了一個項目用到這個功能,所以把我的代碼記錄下來和大家分享,有需求的朋友可以參考下
    2012-12-12
  • Android全局獲取Context實例詳解

    Android全局獲取Context實例詳解

    這篇文章主要介紹了Android全局獲取Context實例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • Android忽略文件實例代碼

    Android忽略文件實例代碼

    這篇文章主要介紹了Android忽略文件的實例代碼,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2018-02-02
  • Android實現(xiàn)動態(tài)自動匹配輸入內(nèi)容功能

    Android實現(xiàn)動態(tài)自動匹配輸入內(nèi)容功能

    這篇文章主要為大家詳細介紹了Android實現(xiàn)動態(tài)自動匹配輸入內(nèi)容功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06

最新評論

德保县| 衢州市| 上饶县| 兰西县| 潢川县| 沙洋县| 武安市| 喀什市| 敦化市| 临泽县| 新乡市| 白河县| 葫芦岛市| 绥滨县| 金堂县| 天水市| 扶风县| 哈巴河县| 永安市| 镇巴县| 崇左市| 临海市| 弥渡县| 若尔盖县| 罗江县| 双牌县| 二手房| 海宁市| 哈尔滨市| 高碑店市| 渝中区| 金坛市| 杭锦旗| 红原县| 武隆县| 民权县| 焦作市| 射阳县| 连云港市| 东方市| 理塘县|