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

android选择视频文件上传到后台服务器,第1张

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

本文实例为大家分享了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选择视频文件上传到后台服务器全部内容,希望文章能够帮你解决android选择视频文件上传到后台服务器所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1146310.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存