android选择视频文件上传到后台服务器
作者:风晴03 发布时间:2023-06-11 22:50:44
标签: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) {
}


猜你喜欢
- 实现控件拖动的基本原理是对鼠标位置的捕获,同时根据鼠标按键的按下、释放确定控件移动的幅度和时机。 简单示例: 在Grid中有一个Button
- Android中的异步消息机制分为四个部分:Message、Handler、MessageQueue和Looper。其中,Message是线
- 一、进程内部的线程同步1、使用lock,用法如下:private static readonly object SeqLock = new
- 1.概述我们之前讨论过Java Generics的基础知识。在本文中,我们将了解Java中的通用构造函数。 泛型构造函数是至少需要有一个泛型
- public string NextString(int charLowerBound, int charUpperBound, int l
- 以一个web项目为例,代码是可以移植的首先要导入mail.jar包,然后创建自己的类1:HTMLSender类package com.txq
- 简介Spring Security 是为了基于Spring的应用程序提供的声明式安全保护的安全性框架。Spring Security 提供了
- 扫码枪扫码效果等同于键盘录入,会回调dispatchKeyEvent键盘按下事件。开发环境:有线扫码枪,支持二维码代码1. 接收数据 /**
- 什么是委托?之前写了事件的介绍:https://www.jb51.net/article/59461.htm这里也把委托相关知识也总结一下。
- DatagramSocket只允许数据报发送给指定的目标地址,而MulticastSocket可以将数据报以广播的方式发送至多个客户端。其主
- 1. 多行编辑先来体验一下从xml文件拷贝字段新建实体对象一般我们为了新建多表连接后映射的 ResultMap ,耗费不少时间,那么我们就来
- 废话不多说,直接上代码,小伙伴们仔细看 * 释吧。/*简单的复制 剪切 粘贴 功能 操作: &nb
- (Memory Leak,内存泄漏)为什么会产生内存泄漏?当一个对象已经不需要再使用本该被回收时,另外一个正在使用的对象持有它的引用从而导致
- 本文实例讲述了Java实现读取及生成Excel文件的方法。分享给大家供大家参考,具体如下:一、读取Excel文件需要先下载poi-3.0.1
- 前言之前我们探讨过一个.class文件是如何被加载到jvm中的。但是jvm内又是如何划分内存的呢?这个内被加载到了那一块内存中?jvm内存划
- /*最小树形图图模版-朱刘算法模版说明:点标号必须0-(N-1) 必须去除到自身的点(到自身的边的边权赋无限大)*/
- 1.使用的注意事项本节给大家带来基础UI控件部分的最后一个控件:DrawerLayout,官方给我们提供的一个侧滑菜单控件,和上一节的Vie
- 静态变量静态变量位于栈上,它是一个全局变量,在编译期就已经生成。public class Cow{public static int cou
- 1.在java代码中(SplashActivity继承AppCompatActivity时无效)2.在manifest.xml中改Theme
- 1. 选择恰当的日志级别常见的日志级别有5种,分别是error、warn、info、debug、trace。日常开发中,我们需要选择恰当的日