• android 读取SD卡文件


    SD卡作为手机的扩展存储设备,在手机中充当硬盘角色,可以让我们手机存放更多的数据以及多媒体等大体积文件。因此查看SD卡的内存就跟我们查看硬盘的剩余空间一样,是我们经常操作的一件事,那么在Android开发中,我们如何能获取SD卡的内存容量呢?

    首先,要获取SD卡上面的信息,必须先对SD卡有访问的权限,因此第一件事就是需要添加访问扩展设备的权限。

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    其次,需要判断手机上面SD卡是否插好,如果有SD卡的情况下,我们才可以访问得到并获取到它的相关信息,当然以下这个语句需要用if做判断。

    Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)

    取得sdcard文件路径

    File path = Environment.getExternalStorageDirectory(); 
    StatFs statfs = new StatFs(path.getPath());

    获取block的SIZE

    long blocSize = statfs.getBlockSize();

    获取BLOCK数量

    long totalBlocks = statfs.getBlockCount();

    空闲的Block的数量

    long availaBlock = statfs.getAvailableBlocks();

    计算总空间大小和空闲的空间大小

    储空间大小跟空闲的存储空间大小就被计算出来了。

    /**
    
    * 取得空闲sd卡空间大小
    
    * @return
    
    */
    
    public long getAvailaleSize(){
    
    File path = Environment.getExternalStorageDirectory(); //取得sdcard文件路径
    
    StatFs stat = new StatFs(path.getPath()); 
    
    /*获取block的SIZE*/
    
    long blockSize = stat.getBlockSize(); 
    
    /*空闲的Block的数量*/
    
    long availableBlocks = stat.getAvailableBlocks();
    
    /* 返回bit大小值*/
    
    return availableBlocks * blockSize; 
    
    //(availableBlocks * blockSize)/1024      KIB 单位
    
    //(availableBlocks * blockSize)/1024 /1024  MIB单位
    
     
    
    }
    
     
    
    /**
    
    * SD卡大小
    
    * @return
    
    */
    
    public long getAllSize(){
    
    File path = Environment.getExternalStorageDirectory(); 
    
    StatFs stat = new StatFs(path.getPath()); 
    
    /*获取block的SIZE*/
    
    long blockSize = stat.getBlockSize(); 
    
    /*块数量*/
    
    long availableBlocks = stat.getBlockCount();
    
    /* 返回bit大小值*/
    
    return availableBlocks * blockSize; 
    
    }
    
     

    Android 文件的浏览(类似于FileDialog的功能)

    首先说一下这个文件浏览的简单实现原理:

    首先选择一个目录做为根目录,然后打开此目录,常用的就是使用File这个类了,如下:

    File file=new File(path);

    然后可以通过获取到此目录下所有文件及文件夹的列表:

    如下:

    File[]  files = file.listFiles();

    然后再根据根据得到的文件,来判断是文件夹还是文件,如果是文件夹,那么我们就将文件夹添加到列表中,如果是文件那么就把文件添加到列表中进行显示,如果需要显示图标的话,那么就需要根据文件的后缀,设置不同的图标了。我是在ListView中进行显示的。

    基本代码如下:

    for (File currentFile : files)
            {
                //判断是一个文件夹还是一个文件
                if (currentFile.isDirectory())
                {
                    currentIcon = getResources().getDrawable(R.drawable.folder);
                }
                else
                {
                    //取得文件名
                    String fileName = currentFile.getName();
                    //根据文件名来判断文件类型,设置不同的图标
                    if (checkEndsWithInStringArray(fileName, getResources().getStringArray(R.array.fileEndingImage)))
                    {
                        currentIcon = getResources().getDrawable(R.drawable.image);
                    }
                    else if (checkEndsWithInStringArray(fileName, getResources().getStringArray(R.array.fileEndingWebText)))
                    {
                        currentIcon = getResources().getDrawable(R.drawable.webtext);
                    }
                    else if (checkEndsWithInStringArray(fileName, getResources().getStringArray(R.array.fileEndingPackage)))
                    {
                        currentIcon = getResources().getDrawable(R.drawable.packed);
                    }
                    else if (checkEndsWithInStringArray(fileName, getResources().getStringArray(R.array.fileEndingAudio)))
                    {
                        currentIcon = getResources().getDrawable(R.drawable.audio);
                    }
                    else if (checkEndsWithInStringArray(fileName, getResources().getStringArray(R.array.fileEndingVideo)))
                    {
                        currentIcon = getResources().getDrawable(R.drawable.video);
                    }
                    else
                    {
                        currentIcon = getResources().getDrawable(R.drawable.text);
                    }
                }

    下面是根据后缀对文件的类型进行的判断:

     //通过文件名判断是什么类型的文件
        private boolean checkEndsWithInStringArray(String checkItsEnd, 
                        String[] fileEndings)
        {
            for(String aEnd : fileEndings)
            {
                if(checkItsEnd.endsWith(aEnd))
                    return true;
            }
            return false;
        }

     再贴一段自己的代码,获取SD卡文件名

    private void initData() {
    
    
    
    // 判断是否有外存储设备
    
    if (Environment.getExternalStorageState().equals(
    
    Environment.MEDIA_MOUNTED)) {
    
    File path = Environment.getExternalStorageDirectory();
    
    File file = new File(path.getPath());
    
    File[] files = file.listFiles();
    
    
    
    getFileName(files);
    
    }
    
    }
    
    
    
    private void getFileName(File[] files) {
    
    
    
    for (File currentFile : files) {
    
    // 判断是一个文件夹还是一个文件
    
    if (currentFile.isDirectory()) {
    
    getFileName(currentFile.listFiles());
    
    } else {
    
    // 取得文件名
    
    String fileName = currentFile.getName();
    
    // 获取.info后缀的文件
    
    if (fileName.endsWith(".info"))
    
    // 保存去掉后缀的文件名
    
    names.add(fileName.substring(0, fileName.lastIndexOf(".")));
    
    }
    
    }
    
    }
  • 相关阅读:
    jQuery 源码基本框架
    jQuery 源码细读 -- $.Callbacks
    Excel等外部程序点击链接会带上IE信息的bug
    &nbsp; 与 空格的区别
    前端模板文件化jQuery插件 $.loadTemplates
    客户端渲染的页面能否被搜索引擎完整收录呢?
    javascript 函数声明问题
    JavaScript 继承机制小记
    link与@import
    tcp_udp
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/5670645.html
Copyright © 2020-2023  润新知