使用 MediaStore获取指定文件夹的视频,更优化方法:
import android.content.ContentResolver;import android.content.ContentValues;import android.content.Context;import android.net.Uri;import android.os.Build;import android.os.Environment;import android.provider.MediaStore;import android.support.annotation.RequiresApi;import android.webkit.MimeTypeMap;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;import java.nio.file.Files;public class VideoUtils {public static final String VIDEO_DIR = "videoData";public static Uri saveVideoToExternalStorage(Context context, File videoFile) throws IOException {if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {// 分区存储模式下需要使用MediaStore APIreturn insertVideoWithMediaStore(context, videoFile);} else {// 旧存储模型可以直接写入文件return writeVideoToExternalFilesDir(context, videoFile);}}@RequiresApi(api = Build.VERSION_CODES.O)private static Uri insertVideoWithMediaStore(Context context, File videoFile) {// 获取文件的MIME类型String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(videoFile.getName().substring(videoFile.getName().lastIndexOf('.')));ContentValues values = new ContentValues();values.put(MediaStore.Video.Media.DISPLAY_NAME, videoFile.getName());values.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");values.put(MediaStore.Video.Media.RELATIVE_PATH, Environment.DIRECTORY_MOVIES + "/" + VIDEO_DIR);ContentResolver resolver = context.getContentResolver();Uri uri = resolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);if (uri != null) {try (OutputStream outputStream = context.getContentResolver().openOutputStream(uri)) {Files.copy(videoFile.toPath(), outputStream);} catch (IOException e) {e.printStackTrace();}}return uri;}public static Uri saveVideoToExternalStorage2(Context context, File videoFile) throws IOException {if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {// 分区存储模式下需要使用MediaStore APIreturn insertVideoWithMediaStore(context, videoFile);} else {// 旧存储模型可以直接写入文件return writeVideoToExternalFilesDir(context, videoFile);}}/** * 将视频文件保存到应用的专属外部存储目录。 * * @param context 应用的 Context * @param videoFile 要保存的视频文件 * @return 保存视频文件的 Uri * @throws IOException 如果文件写入过程中发生错误 */public static Uri writeVideoToExternalFilesDir(Context context, File videoFile) throws IOException {// Get the external files directory for the appFile externalFilesDir = context.getExternalFilesDir(null);if (externalFilesDir != null) {// Create a unique name for the video file in the external directoryFile outputFile = new File(externalFilesDir, getFileName(videoFile.getAbsolutePath())); // Use a meaningful file name// Ensure the parent directory existsoutputFile.getParentFile().mkdirs();// Open a buffered input stream from the source file and a buffered output stream to the destination filetry (FileInputStream inputStream = new FileInputStream(videoFile); BufferedInputStream bufferedInput = new BufferedInputStream(inputStream); FileOutputStream outputStream = new FileOutputStream(outputFile); BufferedOutputStream bufferedOutput = new BufferedOutputStream(outputStream)) {byte[] buffer = new byte[4096];int bytesRead;while ((bytesRead = bufferedInput.read(buffer)) != -1) {bufferedOutput.write(buffer, 0, bytesRead);}}return Uri.fromFile(outputFile);}return null;}}