日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android 与后台实时视频,android选择视频文件上传到后台服务器

發布時間:2024/1/8 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 与后台实时视频,android选择视频文件上传到后台服务器 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文實例為大家分享了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();

files = new ArrayList();

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 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选择视频文件上传到后台服务器的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。