android发广播更新相册,安卓保存视频和图片之后相册不刷新的问题总结

最近在做项目中遇到保存照片和视频,本地已经保存而在相册和项目中不能找到,这里做一个简单的总结

在本地保存之后需要吧文件发送到本地或者广播的方式刷新相册

1.照片发送到相册

//把文件插入到系统图库

/***@paramcontext

*@paramtargetFile 要保存的照片文件

*@parampath 要保存的照片的路径地址*/

public static voidaddMediaStore(Context context, File targetFile, String path) {

ContentResolver resolver=context.getContentResolver();

ContentValues newValues= new ContentValues(5);

newValues.put(MediaStore.Images.Media.DISPLAY_NAME, targetFile.getName());

newValues.put(MediaStore.Images.Media.DATA, targetFile.getPath());

newValues.put(MediaStore.Images.Media.DATE_MODIFIED, System.currentTimeMillis()/ 1000);

newValues.put(MediaStore.Images.Media.SIZE, targetFile.length());

newValues.put(MediaStore.Images.Media.MIME_TYPE,"image/jpeg");

resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, newValues);

MediaScannerConnection.scanFile(context,new String[]{path}, null, null);//刷新相册

}

示例代码

addMediaStore(chatActivity, mFile, mFile.getAbsolutePath());

(上下文,文件,和文件的路径)

2.视频发送到相册

//针对非系统影音资源文件夹

public static void insertIntoMediaStore(Context context, boolean isVideo, File saveFile, longcreateTime) {

ContentResolver mContentResolver=context.getContentResolver();if (createTime == 0)

createTime=System.currentTimeMillis();

ContentValues values= newContentValues();

values.put(MediaStore.MediaColumns.TITLE, saveFile.getName());

values.put(MediaStore.MediaColumns.DISPLAY_NAME, saveFile.getName());//值一样,但是还是用常量区分对待

values.put(isVideo ?MediaStore.Video.VideoColumns.DATE_TAKEN

: MediaStore.Images.ImageColumns.DATE_TAKEN, createTime);

values.put(MediaStore.MediaColumns.DATE_MODIFIED, System.currentTimeMillis());

values.put(MediaStore.MediaColumns.DATE_ADDED, System.currentTimeMillis());if (!isVideo)

values.put(MediaStore.Images.ImageColumns.ORIENTATION,0);

values.put(MediaStore.MediaColumns.DATA, saveFile.getAbsolutePath());

values.put(MediaStore.MediaColumns.SIZE, saveFile.length());

values.put(MediaStore.MediaColumns.MIME_TYPE, isVideo? getVideoMimeType下面的方法/*"video/3gp"*/ : "image/jpeg");//插入

mContentResolver.insert(isVideo?MediaStore.Video.Media.EXTERNAL_CONTENT_URI

: MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

}

//这里是一个调用的示例代码(上下文,是否为视频,文件,时间)

insertIntoMediaStore(chatActivity,true,dest,0);

这个也可作为相册刷新图库

这里需要一个获取视频格式的相关方法

//获取video的mine_type,暂时只支持mp4,3gp

private staticString getVideoMimeType(String path) {

String lowerPath=path.toLowerCase();if (lowerPath.endsWith("mp4") || lowerPath.endsWith("mpeg4")) {return "video/mp4";

}else if (lowerPath.endsWith("3gp")) {return "video/3gp";

}return "video/mp4";

}

3.通用的方法(发送广播来通知相册刷新)

/*** 针对系统文夹只需要扫描,不用插入内容提供者,不然会重复

*

*@paramcontext 上下文

*@paramfilePath 文件路径*/private static voidscanFile(Context context, String filePath) {if (!checkFile(filePath))return;

Intent intent= newIntent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);

intent.setData(Uri.fromFile(newFile(filePath)));

context.sendBroadcast(intent);

}

调用示例(上下文,和文件的路径)

scanFile(chatActivity,/*FileUtil.getCacheFilePath(video_file_name)*/dest.getAbsolutePath());

综上,这里都是系统的方法,根据项目稍微一做修改,可以直接调用根据自己需求稍作修改

leileitua

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值