您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關android選擇視頻文件上傳到后臺服務器的步驟,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
選擇本地視頻文件
附上Demo
首先第一步打開打開相冊選擇視頻文件:
Intent intent = new Intent(); intent.setType("video/*"); intent.setAction(Intent.ACTION_GET_CONTENT); intent.addCategory(Intent.CATEGORY_OPENABLE); ((Activity) ctx).startActivityForResult(intent, ProfilePhotoTask.PHOTO_CAMERA);
ProfilePhotoTask.PHOTO_CAMERA為請求返回碼
第二步處理返回結(jié)果:
/** * 視頻回調(diào) */ @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case ProfilePhotoTask.PHOTO_CAMERA: if (resultCode == Activity.RESULT_OK) { try { Uri uri = data.getData(); uri = BitmapCache.geturi(this, data); path = getPath(uri); File file = new File(path); if (!file.exists()) { break; } if (file.length() > 100 * 1024 * 1024) { commonToast("文件大于100M"); break; } //傳換文件流,上傳 submitVedio(); } catch (Exception e) { } catch (OutOfMemoryError e) { } } break; } }
第三步轉(zhuǎn)換文件為流進行上傳:這種把文件全讀到內(nèi)存中,易內(nèi)存泄露。已經(jīng)修改為斷點續(xù)傳,參見開篇demo
try { fInfos = new ArrayList<PhoneUploadFileInfo>(); files = new ArrayList<ByteArrayInputStream>(); PhoneUploadFileInfo fInfo = new PhoneUploadFileInfo(); fInfo.setFileType(path.substring(path.lastIndexOf(".") + 1)); fInfo.setOriginalName(path.substring(path .lastIndexOf("/") + 1)); ByteArrayInputStream ins = FileUtil .getByteArrayInputStream(new File(path)); files.add(ins); // 上傳文件其他信息 fInfos.add(fInfo); ins = null; } catch (Exception ex) { String a = ex + ""; }
視頻文件轉(zhuǎn)換為流方法:
public static ByteArrayInputStream getByteArrayInputStream(File file){ return new ByteArrayInputStream(getByetsFromFile(file)); } /** * ByteArrayInputStream ins = new ByteArrayInputStream(picBytes); * @param file * @return */ public static byte[] getByetsFromFile(File file){ FileInputStream is = null; // 獲取文件大小 long length = file.length(); // 創(chuàng)建一個數(shù)據(jù)來保存文件數(shù)據(jù) byte[] fileData = new byte[(int)length]; try { is = new FileInputStream(file); } catch (FileNotFoundException e) { e.printStackTrace(); } int bytesRead=0; // 讀取數(shù)據(jù)到byte數(shù)組中 while(bytesRead != fileData.length) { try { bytesRead += is.read(fileData, bytesRead, fileData.length - bytesRead); if(is != null) is.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return fileData; }
斷點續(xù)傳核心代碼:
try { File file = new File(path); FileInputStream is = null; // 獲取文件大小 long length = file.length(); // 創(chuàng)建一個數(shù)據(jù)來保存文件數(shù)據(jù) byte[] fileData = null; try { is = new FileInputStream(file); } catch (FileNotFoundException e) { e.printStackTrace(); } // 讀取數(shù)據(jù)到byte數(shù)組中 List<ByteArrayInputStream> temp = new ArrayList<>(); int len = 0; fileData = new byte[1000 * 1000 * 2]; //斷點續(xù)傳 while ((len = is.read(fileData)) != -1) { temp = new ArrayList<>(); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(fileData); temp.add(byteArrayInputStream); //這里是提交數(shù)組流到后臺 // RegisterControlService.submitVedioSon( // SubVedioViewActivity.this, temp, fInfos, subIdx); temp.clear(); byteArrayInputStream.close(); } if (is != null) is.close(); } catch (Exception ex) { }
關于android選擇視頻文件上傳到后臺服務器的步驟就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。