4、android 调用MediaRecorder录制视频,图库中找不到保存视频

前言:这个问题挺尴尬的,其实是保存上了,但是没有在系统图库中显示

一、生成保存路径

    private String createRecordPath() {
        sdDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES);
        if (sdDir != null) {
            File dir = new File(sdDir.getAbsolutePath());//新建子目录
            if (!dir.exists()) {
                dir.mkdirs();
            }
            //视频文件的路径
            String path = dir.getAbsolutePath() + "/" + System.currentTimeMillis() + ".mp4";
            return path;
        }
        return null;
    }

使用时间作为文件名,保存格式为MP4。

然后调用MediaRecorder类的setOutputFile()方法将视频保存上,从上面代码中可以看到保存的文件名为Movies,完整路径为:/storage/emulated/0/Movies,打开手机文件系统是可以找到录制的视频的,但是打开图库却没有,查了下资料,需要作下修改。

二、图库中显示录制视频

方法1、保存成功后发一个系统广播通知手机又图片更新,广播如下:

Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri uri = Uri.fromFile(file);
intent.setData(uri);
context.sendBroadcast(intent);//这个广播的目的就是更新图库,发了这个广播进入相册就可以找到你保存的图片了!,记得要传你更新的file哦

方法2、通过FileOutputStream 进行写入保存

//保存方法 
public void saveBitmap() { 
Log.e(TAG, "保存图片"); 
File f = new File("/sdcard/namecard/", picName); 
if (f.exists()) { 
f.delete(); 
} 
try { 
FileOutputStream out = new FileOutputStream(f); 
bm.compress(Bitmap.CompressFormat.PNG, 90, out); 
out.flush(); 
out.close(); 
Log.i(TAG, "已经保存"); 
} catch (FileNotFoundException e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 
} catch (IOException e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 
} 
} 

ps:在这之前还需要获取保存权限,安卓6之后需要动态获取下。

另外,使用MediaRecorder录制视频时,会出现闪退的问题,笔者出错的问题是设置的MediaRecorder参数不对,更改后就好了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值