安卓(Android) 怎么获取外部视频(Video)文件

2019年10月18日22:01:40安卓(Android) 怎么获取外部视频(Video)文件已关闭评论 286 1268字阅读4分13秒

1.简介

安卓(Android) 怎么获取外部视频(Video)文件?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */

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);
        return externalVideoFile;
    }
    private static File getStorageFolder(Context context) {
        
        File storageFolder = context.getExternalFilesDir(null);
        if (storageFolder == null) {
            
            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();
        }
    }
}

 
继续阅读
编程十万个怎么办