国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

android選擇視頻文件上傳到后臺服務器

2019-10-23 18:30:09
字體:
來源:轉載
供稿:網友

本文實例為大家分享了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為請求返回碼

第二步處理返回結果:

 /**   * 視頻回調   */  @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;    }  }

第三步轉換文件為流進行上傳:這種把文件全讀到內存中,易內存泄露。已經修改為斷點續傳,參見開篇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 + "";          }

視頻文件轉換為流方法:

 

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();    // 創建一個數據來保存文件數據    byte[] fileData = new byte[(int)length];    try {      is = new FileInputStream(file);    } catch (FileNotFoundException e) {      e.printStackTrace();    }    int bytesRead=0;    // 讀取數據到byte數組中    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;  }

斷點續傳核心代碼:

try {            File file = new File(path);            FileInputStream is = null;            // 獲取文件大小            long length = file.length();            // 創建一個數據來保存文件數據            byte[] fileData = null;            try {              is = new FileInputStream(file);            } catch (FileNotFoundException e) {              e.printStackTrace();            }            // 讀取數據到byte數組中            List<ByteArrayInputStream> temp = new ArrayList<>();            int len = 0;            fileData = new byte[1000 * 1000 * 2];            //斷點續傳            while ((len = is.read(fileData)) != -1) {              temp = new ArrayList<>();              ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(fileData);              temp.add(byteArrayInputStream);              //這里是提交數組流到后臺//              RegisterControlService.submitVedioSon(//                  SubVedioViewActivity.this, temp, fInfos, subIdx);              temp.clear();              byteArrayInputStream.close();            }            if (is != null)              is.close();          } catch (Exception ex) { }

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 浦东新区| 浮山县| 海兴县| 高唐县| 娱乐| 定结县| 镇安县| 静乐县| 桂林市| 晋江市| 永兴县| 融水| 吉木萨尔县| 章丘市| 云林县| 公主岭市| 鹤峰县| 泰顺县| 锡林郭勒盟| 隆化县| 琼中| 宣汉县| 同江市| 镇坪县| 济宁市| 乌海市| 东城区| 沾益县| 察雅县| 滦南县| 南昌县| 修武县| 丰顺县| 红河县| 凯里市| 罗定市| 和硕县| 慈溪市| 平顶山市| 襄汾县| 廊坊市|