温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

android选择视频文件上传到后台服务器的步骤

发布时间:2020-06-23 15:23:00 来源:亿速云 阅读:603 作者:清晨 栏目:移动开发

这篇文章将为大家详细讲解有关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) {
 }

关于android选择视频文件上传到后台服务器的步骤就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI