安卓保存视频到相册
创始人
2024-09-01 18:30:16
0

在安卓上保存视频到相册可以通过以下步骤来实现:

  1. 确保在AndroidManifest.xml文件中添加以下权限:

  1. 在代码中创建一个保存视频的方法,例如:
private void saveVideoToGallery(String videoPath) {
    File videoFile = new File(videoPath);
    if (!videoFile.exists()) {
        Toast.makeText(this, "视频文件不存在", Toast.LENGTH_SHORT).show();
        return;
    }

    // 获取存储的目录,通常为 "/storage/emulated/0/DCIM/Camera/"
    File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM + "/Camera");
    if (!storageDir.exists()) {
        storageDir.mkdirs();
    }

    String fileName = "video_" + System.currentTimeMillis() + ".mp4";
    File destFile = new File(storageDir, fileName);
    try {
        FileInputStream fis = new FileInputStream(videoFile);
        FileOutputStream fos = new FileOutputStream(destFile);
        byte[] buffer = new byte[1024];
        int length;
        while ((length = fis.read(buffer)) > 0) {
            fos.write(buffer, 0, length);
        }
        fos.flush();
        fos.close();
        fis.close();

        // 通知相册刷新
        MediaScannerConnection.scanFile(this, new String[]{destFile.getAbsolutePath()}, null, null);

        Toast.makeText(this, "视频保存成功", Toast.LENGTH_SHORT).show();
    } catch (IOException e) {
        e.printStackTrace();
        Toast.makeText(this, "视频保存失败", Toast.LENGTH_SHORT).show();
    }
}
  1. 调用保存视频的方法,传入视频文件路径:
saveVideoToGallery("/path/to/video.mp4");

这样就可以将视频保存到相册中了。请注意,保存视频需要WRITE_EXTERNAL_STORAGE权限,并且在Android 10及以上版本中,还需要动态请求此权限。

相关内容

热门资讯

推荐透视!德普之星透视,德普之... 推荐透视!德普之星透视,德普之星有透视辅助吗,切实有辅助脚本(哔哩哔哩)1、操作简单,无需手机版透视...
教你透视!aapoker透视脚... 教你透视!aapoker透视脚本,aapoker插件下载,好像一直总是有辅助攻略(哔哩哔哩)1、辅助...
关于透视!wpk透视辅助靠谱吗... 关于透视!wpk透视辅助靠谱吗,wpk透视是真的吗,切实是有辅助攻略(哔哩哔哩)所有人都在同一条线上...
普及透视!aapoker俱乐部... 普及透视!aapoker俱乐部靠谱吗,aapoker俱乐部靠谱吗,真是真的是有辅助方法(哔哩哔哩)1...
科普透视!hhpoker脚本下... 科普透视!hhpoker脚本下载,hhpoker辅助软件,总是真的是有辅助脚本(哔哩哔哩)该软件可以...
解迷透视!hhpkoer辅助挂... 解迷透视!hhpkoer辅助挂是真的吗,hhpoker可以控制牌吗,一直真的有辅助脚本(哔哩哔哩)1...
科普透视!hhpoker怎么开... 科普透视!hhpoker怎么开透视,hhpoker脚本下载,原来一直都是有辅助工具(哔哩哔哩)运辅助...
必备透视!hhpoker辅助挂... 必备透视!hhpoker辅助挂,hhpoker买挂,其实存在有辅助教程(哔哩哔哩)1、下载好正确养号...
了解透视!aapoker透视插... 了解透视!aapoker透视插件,aapoker辅助器是真的吗,果然真的有辅助插件(哔哩哔哩)辅助器...
有挂透视!we-poker软件... 有挂透视!we-poker软件,wepoker辅助透视,真是一直都是有辅助软件(哔哩哔哩)1、完成有...