get External Video File - Android App

Android examples for App:External Storage

Description

get External Video File

Demo Code


import android.content.Context;
import android.content.res.Resources;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class Main{
    private final static String VIDEO_FILE_NAME = "sample_mpeg4.mp4";
    private final static int BUFFER_SIZE = 1024 * 1024;
    public static File getExternalVideoFile(Context context) {
        File externalFilesDir = getStorageFolder(context);
        File externalVideoFile = new File(externalFilesDir, VIDEO_FILE_NAME);
        copyFileFromAssets(context, VIDEO_FILE_NAME,
                externalVideoFile);/*from   w w  w  . ja  v  a  2  s. c o  m*/
        return externalVideoFile;
    }
    private static File getStorageFolder(Context context) {
        // Get external storage folder
        File storageFolder = context.getExternalFilesDir(null);
        if (storageFolder == null) {
            // If failed, get internal storage folder
            storageFolder = context.getFilesDir();
        }
        return storageFolder;
    }
    public static void copyFileFromAssets(Context context, String fileName,
            File outputFile) {
        byte[] buffer = new byte[BUFFER_SIZE];
        int bytesCount;
        try {
            InputStream imageStream = context.getAssets().open(fileName);
            FileOutputStream fileOutputStream = new FileOutputStream(
                    outputFile);
            while ((bytesCount = imageStream.read(buffer)) >= 0) {
                fileOutputStream.write(buffer, 0, bytesCount);
            }
            fileOutputStream.close();
            imageStream.close();
        } catch (IOException | Resources.NotFoundException e) {
            e.printStackTrace();
        }
    }
}

Related Tutorials