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

android選擇視頻文件上傳到后臺(tái)服務(wù)器

 更新時(shí)間:2020年06月16日 15:54:51   作者:風(fēng)晴03  
這篇文章主要介紹了android選擇視頻文件上傳到后臺(tái)服務(wù)器的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了android選擇視頻文件上傳到后臺(tái)服務(wù)器的具體代碼,供大家參考,具體內(nèi)容如下

選擇本地視頻文件

附上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)換文件為流進(jìn)行上傳:這種把文件全讀到內(nèi)存中,易內(nèi)存泄露。已經(jīng)修改為斷點(diǎn)續(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)建一個(gè)數(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;
 }

斷點(diǎn)續(xù)傳核心代碼:

try {
      File file = new File(path);
      FileInputStream is = null;
      // 獲取文件大小
      long length = file.length();
      // 創(chuàng)建一個(gè)數(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];
      //斷點(diǎn)續(xù)傳
      while ((len = is.read(fileData)) != -1) {
       temp = new ArrayList<>();
       ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(fileData);
       temp.add(byteArrayInputStream);
       //這里是提交數(shù)組流到后臺(tái)
//       RegisterControlService.submitVedioSon(
//         SubVedioViewActivity.this, temp, fInfos, subIdx);
       temp.clear();
       byteArrayInputStream.close();
      }
      if (is != null)
       is.close();
     } catch (Exception ex) {
 }

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

相關(guān)文章

  • 使用kotlin協(xié)程提高app性能(譯)

    使用kotlin協(xié)程提高app性能(譯)

    這篇文章主要介紹了使用kotlin協(xié)程提高app性能(譯),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • android利用消息機(jī)制獲取網(wǎng)絡(luò)圖片

    android利用消息機(jī)制獲取網(wǎng)絡(luò)圖片

    這篇文章主要為大家詳細(xì)介紹了android利用消息機(jī)制獲取網(wǎng)絡(luò)圖片的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • 解決Android MediaRecorder錄制視頻過短問題

    解決Android MediaRecorder錄制視頻過短問題

    本文主要介紹Android MediaRecorder,在使用MediaRecorder時(shí)經(jīng)常會(huì)遇到視頻錄制太短問題,這里提供解決問題的實(shí)例代碼以供大家參考
    2016-07-07
  • Android ListView 實(shí)現(xiàn)上拉加載的示例代碼

    Android ListView 實(shí)現(xiàn)上拉加載的示例代碼

    這篇文章主要介紹了Android ListView 實(shí)現(xiàn)上拉加載的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-07-07
  • Android 九宮格的實(shí)現(xiàn)方法

    Android 九宮格的實(shí)現(xiàn)方法

    今天在瀏覽網(wǎng)頁的時(shí)候看到了一篇有關(guān)九宮格實(shí)現(xiàn)的博文,感覺挺有意思。所以自己模仿做了一個(gè),先貼出代碼如下:
    2013-05-05
  • Android中的Permission權(quán)限機(jī)制介紹

    Android中的Permission權(quán)限機(jī)制介紹

    這篇文章主要介紹了Android中的Permission權(quán)限機(jī)制介紹,本文講解了權(quán)限策略、權(quán)限聲明、權(quán)限請求、獲取權(quán)限等內(nèi)容,需要的朋友可以參考下
    2015-04-04
  • Android中通知Notification的使用方法

    Android中通知Notification的使用方法

    這篇文章主要為大家詳細(xì)介紹了Android中通知Notification的使用方法,感興趣的小伙伴們可以參考一下
    2016-08-08
  • Gradle的緩存路徑修改的四種方法(小結(jié))

    Gradle的緩存路徑修改的四種方法(小結(jié))

    這篇文章主要介紹了Gradle的緩存路徑修改的四種方法(小結(jié)),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • Android進(jìn)階Handler應(yīng)用線上卡頓監(jiān)控詳解

    Android進(jìn)階Handler應(yīng)用線上卡頓監(jiān)控詳解

    這篇文章主要為大家介紹了Android進(jìn)階Handler應(yīng)用線上卡頓監(jiān)控詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • Android之ProgressBar即時(shí)顯示下載進(jìn)度詳解

    Android之ProgressBar即時(shí)顯示下載進(jìn)度詳解

    這篇文章主要為大家詳細(xì)介紹了Android之ProgressBar即時(shí)顯示下載進(jìn)度,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-09-09

最新評論

连平县| 额济纳旗| 苏尼特右旗| 靖州| 永安市| 茶陵县| 即墨市| 延边| 新乡市| 镶黄旗| 乾安县| 蒙城县| 翼城县| 拉孜县| 莎车县| 和龙市| 永寿县| 阳信县| 奉化市| 如东县| 长武县| 六安市| 五常市| 威远县| 北京市| 神木县| 蒙山县| 阳谷县| 鸡泽县| 平舆县| 大连市| 无锡市| 石林| 吉首市| 泊头市| 阿克苏市| 东兴市| 北海市| 铜梁县| 沈阳市| 佳木斯市|