android選擇視頻文件上傳到后臺(tái)服務(wù)器
本文實(shí)例為大家分享了android選擇視頻文件上傳到后臺(tái)服務(wù)器的具體代碼,供大家參考,具體內(nèi)容如下
選擇本地視頻文件
附上Demo
首先第一步打開(kāi)打開(kāi)相冊(cè)選擇視頻文件:
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為請(qǐng)求返回碼
第二步處理返回結(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ù)傳,參見(jiàn)開(kāi)篇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ù)來(lái)保存文件數(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ù)來(lái)保存文件數(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) {
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android使用post方式上傳圖片到服務(wù)器的方法
- android 上傳文件到服務(wù)器代碼實(shí)例
- Android實(shí)現(xiàn)上傳圖片至java服務(wù)器
- Android選擇圖片或拍照?qǐng)D片上傳到服務(wù)器
- Android中實(shí)現(xiàn)OkHttp上傳文件到服務(wù)器并帶進(jìn)度
- Android實(shí)現(xiàn)上傳文件到服務(wù)器實(shí)例詳解
- Android使用OKHttp庫(kù)實(shí)現(xiàn)視頻文件的上傳到服務(wù)器功能
- android 開(kāi)發(fā)中使用okhttp上傳文件到服務(wù)器
- Android 通過(guò)Base64上傳圖片到服務(wù)器實(shí)現(xiàn)實(shí)例
- android?studio實(shí)現(xiàn)上傳圖片到j(luò)ava服務(wù)器
相關(guān)文章
使用genymotion訪問(wèn)本地上Tomcat上數(shù)據(jù)的方法
下面小編就為大家?guī)?lái)一篇使用genymotion訪問(wèn)本地上Tomcat上數(shù)據(jù)的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03
Android usb設(shè)備權(quán)限查詢(xún)及自動(dòng)獲取詳解流程
本篇文章介紹了我想要獲取Android系統(tǒng)usb設(shè)備使用權(quán)限時(shí)遇到的問(wèn)題,以及解決該問(wèn)題的過(guò)程及思路,通讀本篇對(duì)大家的學(xué)習(xí)或工作具有一定的價(jià)值,需要的朋友可以參考下2021-10-10
Android RecyclerView打造自動(dòng)循環(huán)效果
這篇文章主要為大家詳細(xì)介紹了android RecyclerView打造自動(dòng)循環(huán)效果,非常實(shí)用的循環(huán)滾動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
Android編程之DatePicker和TimePicke簡(jiǎn)單時(shí)間監(jiān)聽(tīng)用法分析
這篇文章主要介紹了Android編程之DatePicker和TimePicke簡(jiǎn)單時(shí)間監(jiān)聽(tīng)用法,結(jié)合具體實(shí)例形式分析了時(shí)間控件DatePicker和TimePicke布局與具體功能實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-02-02
Android自定義控件實(shí)現(xiàn)手勢(shì)密碼
這篇文章主要介紹了Android自定義控件實(shí)現(xiàn)手勢(shì)密碼的相關(guān)資料,實(shí)現(xiàn)手勢(shì)解鎖功能,感興趣的小伙伴們可以參考一下2016-07-07
Android項(xiàng)目仿UC瀏覽器和360手機(jī)衛(wèi)士消息常駐欄(通知欄)
本篇文章主要介紹了Android項(xiàng)目仿UC瀏覽器和360手機(jī)衛(wèi)士消息常駐欄(通知欄),可以仿照360的通知欄,有興趣的可以了解一下。2016-11-11

