Android 判断文件是否存在、文件删除文件、文件大小file,删除图片、视频后更新系统相册

删除图片后,更新系统相册
    public void deleteVideoInSystem(String paths) {
        String where=MediaStore.Audio.Media.DATA+" like \""+paths+"%"+"\"";
        
//图片
mContext.getContentResolver().delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,where,null);
   
//视频
  mContext.getContentResolver().delete(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,where,null);
   
 }




/**
     * 判断文件是否存在
     * */
    public boolean fileIsExists(String strFile) {
        try {
            File f=new File(strFile);
            if(!f.exists()) {
                return false;
            }
        }
        catch (Exception e) {
            return false;
        }
        return true;
    }


    /**
     * 根据目录删除所有的文件
     *
     * @param filePath
     */
    public static void deleteFileByPath(String filePath) {
        File rootfile = new File(filePath);
        if (rootfile != null && rootfile.exists()) {
            if (rootfile.isDirectory()) {
                File[] files = rootfile.listFiles();
                if (files != null) {
                    for (File childFile : files) {
                        deleteFileByPath(childFile.getAbsolutePath());
                    }
                }
                rootfile.delete();
            } else if (rootfile.isFile()) {
                rootfile.delete();
            }
        }
    }

    /**
     * 删除文件
     * @param path
     */
    public static void deleteFile(String path){
        File file = new File(path);
        if (file.exists()) {
            file.delete();
        }
    }


   /**
     * 获取某个文件或者目录的大小
     *
     * @param f
     * @return
     */
    public static long getFileSize(File f) {
        long size = 0;
        if (f.exists()) {
            if (f.isDirectory()) {
                File flist[] = f.listFiles();
                if (flist != null) {
                    for (int j = 0; j < flist.length; j++) {

                        if (flist[j].isDirectory()) {
                            size = size + getFileSize(flist[j]);
                        } else {
                            size = size + flist[j].length();
                        }
                    }

                }
            } else {
                size = f.length();
            }
        }
        return size;
    }

获取文件列表

  public static List<String> getFileList(File f) {
        List<String> list = new ArrayList<>();
        if (f.exists()) {
            if (f.isDirectory()) {
                File flist[] = f.listFiles();
                if (flist != null) {
                    for (int j = 0; j < flist.length; j++) {
                        if (flist[j].isDirectory()) {
                            list.addAll(getFileList(flist[j]));
                        } else {
                            list.add(flist[j].getAbsolutePath());
                        }
                    }

                }
            } else {
                list.add(f.getAbsolutePath());
            }
        }
        return list;
    }


 /**
     * 文件大小格式化
     * @param size 单位为B、kb、mb、gb转换
     * @return
     */
    public static String byteToFormat(long size){
        long kb = 1024;
        long mb = kb*1024;
        long gb = mb*1024;
        if (size >= gb){
            return BigDecimalUtils.div(size+"",gb+"",2)+"GB";
        }else if (size >= mb){
            return BigDecimalUtils.div(size+"",mb+"",2)+"MB";

        }else if (size > kb){
            return BigDecimalUtils.div(size+"",kb+"",2)+"KB";
        }else {
            return BigDecimalUtils.div(size+"",1+"",2)+"B";
        }
    }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jian11058

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值