【Android】MediaStore三种刷新方式简介

本文介绍了Android中MediaStore的三种刷新方式:1) 使用MediaStore内部类插入图片;2) 发送ACTION_MEDIA_SCANNER_SCAN_FILE广播更新;3) 通过MediaScannerConnection的scanFile方法批量扫描。此外,还提到了避免目录被扫描的方法——创建.nomedia文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、Media是MediaStore的内部类

(MediaStore.Image.Media),它提供了几个insertImage方法,以方便我们想MediaStore插入图片数据,该操作会生成一张缩略图。插入有两个重载方法,一个是传入图片路径,一个是传入Bitmap;

区别:

传入图片路径的方法,会将制定的图片拷贝一份到Picture目录下,也就是SDCARD中共有两种相同的图片。

传入Bitmap的方法,会将图片直接保存到Picture目录下。

/**
 * Insert an image and create a thumbnail for it.
 *
 * @param cr The content resolver to use
 * @param imagePath The path to the image to insert
 * @param name The name of the image
 * @param description The description of the image
 * @return The URL to the newly created image
 * @throws FileNotFoundException
 */
public static final String insertImage(ContentResolver cr, String imagePath,
        String name, String description) throws FileNotFoundException {
   
    // Check if file exists with a FileInputStream
 FileInputStream stream = new FileInputStream(imagePath);
    try {
   
        Bitmap bm = BitmapFactory.decodeFile(imagePath);
        String ret = insertImage(cr, bm, name, description);
        bm.recycle();
        return ret;
    } finally {
   
        try {
   
            stream.close();
        } catch (IOException e) {
   
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值