当前位置: 移动技术网 > IT编程>移动开发>Android > android选择视频文件上传到后台服务器

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

2019年07月24日  | 移动技术网IT编程  | 我要评论

模型飞机大战,周晟乐博客,新宁红网

本文实例为大家分享了android选择视频文件上传到后台服务器的具体代码,供大家参考,具体内容如下

选择本地视频文件

附上

首先第一步打开打开相册选择视频文件:

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) {
 }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网