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) {
                    }
  • 12
    点赞
  • 60
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风晴03

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值