Takes Uri of a video and returns video in form of bytes - Android Network

Android examples for Network:Uri

Description

Takes Uri of a video and returns video in form of bytes

Demo Code


//package com.java2s;

import java.io.ByteArrayOutputStream;

import java.io.FileNotFoundException;

import java.io.IOException;
import java.io.InputStream;

import android.content.Context;

import android.net.Uri;

public class Main {
    /**// w w  w  .  j  a  va 2  s. co m
     * Takes Uri of a video and returns video in form of bytes
     * @param uri
     * @return
     */
    public static byte[] getBytesFromVideo(Context context, Uri uri) {
        InputStream inputStream = null;

        try {
            inputStream = context.getContentResolver().openInputStream(uri);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        ByteArrayOutputStream bytes = new ByteArrayOutputStream();

        int bufferSize = 1024;
        byte[] buffer = new byte[bufferSize];

        int len = 0;
        try {
            while ((len = inputStream.read(buffer)) != -1)
                bytes.write(buffer, 0, len);

        } catch (IOException e) {
            e.printStackTrace();
        }

        return bytes.toByteArray();
    }
}

Related Tutorials