外部存储

来源:互联网 发布:自身投资知乎 编辑:程序博客网 时间:2024/05/18 10:48

/**
* 外部存储
* 1.外部存储设备不见得总是可用 使用外部存储时需要判断设备的可用性
* 2.外部存储中的文件设备中app都可以访问
* 3.外部存储的适用情况:
* 如果文件允许全局可读写或者是安全性要求并不高时可以存放到外部存储
* 4.对外部存储进行更细致的划分:
* 公共的外部存储 私有的外部存储
* 当app卸载时私有外部存储的文件会随着app的卸载而被删除
*/

public class ExternalStorgaUtils {    /**     * 检测外部存储设备是否可用     * @return 是否可用     */     public static boolean isExternalStorageUseFul(){         boolean bl=false;         //Environment 环境类  获取外部存储的访问状态         String state=Environment.getExternalStorageState();         if(Environment.MEDIA_MOUNTED.equals(state)){             bl=true;         }         return bl;     }    /**     * 根据type类型判断该file文件是否存在     * @param type 文件类型     * @return 是否存在     */    public static boolean hasExternalStorageType(String type){        boolean bl=false;        if(type!=null && !"".equals(type)){            File file=Environment.getExternalStoragePublicDirectory(type);            if(file.exists()){                bl=true;            }        }        return bl;    }    /**     * 向外部存储的公共路径写入文件     * @param type 文件夹类型     * @param fileName 文件名称     * @param content 文件内容字节数组     * @return 是否写入成功     */    public static boolean writeExternalStoragePublic(String type,String fileName,byte[] content){        boolean bl=false;        if(isExternalStorageUseFul()){//外部存储设备可用           if(hasExternalStorageType(type)){// type指定的路径存在               try{                   //getExternalStoragePublicDirectory(Type)                   //根据参数指定的类型构建外部存储的公共路径file对象                   //注意:type不能为null  指定的type表示的是sdcard下的文件夹     sdcard/type/                   File parentFile=Environment.getExternalStoragePublicDirectory(type);                   File file=new File(parentFile,fileName);                   FileOutputStream outputStream=new FileOutputStream(file);                   outputStream.write(content);                   outputStream.close();                   bl=true;               }catch (Exception e){                   e.printStackTrace();               }           }        }        return bl;    }    /**     * 从外部存储的公共路径读取数据     * @param type 文件夹类型     * @param fileName 文件名称     * @return 读取文件字节数组     */    public static byte[] readExternalStoaragePublic(String type,String fileName){        ByteArrayOutputStream outputStream=new ByteArrayOutputStream();        try {            if(isExternalStorageUseFul()){                if(hasExternalStorageType(type)){                    File parentFile=Environment.getExternalStoragePublicDirectory(type);                    File file=new File(parentFile,fileName);                    FileInputStream inputStream=new FileInputStream(file);                    int temp=0;                    byte[] buff=new byte[1024];                    while((temp=inputStream.read(buff))!=-1){                        outputStream.write(buff,0,temp);                    }                    inputStream.close();                }            }        }catch (Exception e){            e.printStackTrace();        }        return outputStream.toByteArray();    }    /**     * 向外部存储的私有路径写入文件     * @param context  上下文     * @param type  文件夹类型     * @param fileName 文件名称     * @param content  文件字节数组     * @return 是否写入成功     *     * context.getExternalFilesDir(type) type为null和不为null的区别     * type表示写入文件存储文件夹的类型     *  如果指定type 会根据type的名称创建文件夹并且写入数据     *     *  例如:type  为 Environment.DIRECTORY_MUSIC 那么     *  storage/sdcard/Android/data/应用程序包名/files/music/xxx     *     *  type  设置为null   storage/sdcard/Android/data/应用程序包名/files/xx     *     */    public static boolean writeExternalStoragePrivate(Context context,String type,                                                      String fileName,byte[] content){        boolean bl=false;        try {            if(isExternalStorageUseFul()){                //getExternalFilesDir(type) 获取外部存储私有路径file对象  type表示文件夹类型                File parentFile=context.getExternalFilesDir(type);                File file=new File(parentFile,fileName);                FileOutputStream outputStream=new FileOutputStream(file);                outputStream.write(content);                outputStream.close();                bl=true;            }        }catch (Exception e){            e.printStackTrace();        }        return bl;    }    /**     *  读取外部存储私有路径的文件     * @param context 上下文     * @param type 文件夹类型     * @param fileName 文件名称     * @return 文件字节数组     */    public static byte[] readExternalStoragePrivate(Context context,String type,String fileName){        ByteArrayOutputStream outputStream=new ByteArrayOutputStream();        try {            if(isExternalStorageUseFul()){                File parentFile=context.getExternalFilesDir(type);                File file=new File(parentFile,fileName);                FileInputStream inputStream=new FileInputStream(file);                int temp=0;                byte[] buff=new byte[1024];                while((temp=inputStream.read(buff))!=-1){                    outputStream.write(buff,0,temp);                }                inputStream.close();            }        }catch (Exception e){            e.printStackTrace();        }        return outputStream.toByteArray();    }    /**     *  向sdcard根目录写入文件     * @param fileName 文件名称     * @param content 文件字节数组     * @return 写入是否成功     */    public static boolean writeSdcardRoot(String fileName,byte[] content){        boolean bl=false;        if(isExternalStorageUseFul()){            try {                //getExternalStorageDirectory()获取sdcard根目录file对象                File parentFile=Environment.getExternalStorageDirectory();                File file=new File(parentFile,fileName);                FileOutputStream outputStream=new FileOutputStream(file);                outputStream.write(content);                outputStream.close();                bl=true;            }catch (Exception e){                e.printStackTrace();            }        }        return bl;    }    /**     *  读取sdcard中的文件     * @param fileName 文件名称     * @return 返回读取的字节数组     */    public static byte[] readSdcardRoot(String fileName){        ByteArrayOutputStream outputStream=new ByteArrayOutputStream();        if(isExternalStorageUseFul()){            try{                File parentFile=Environment.getExternalStorageDirectory();                File file=new File(parentFile,fileName);                FileInputStream inputStream=new FileInputStream(file);                int temp=0;                byte[] buff=new byte[1024];                while((temp=inputStream.read(buff))!=-1){                    outputStream.write(buff,0,temp);                }                inputStream.close();            }catch (Exception e){                e.printStackTrace();            }        }        return outputStream.toByteArray();    }    /**     * 获取file指定路径的空间     * @param file 指定路径file对象     * @return     *     *  getTotalSpace() 返回字节数     *     *  字节 byte b 比特位  1byte=8bit     *  1kb=1024b     *     *  MB 兆字节     *  1Mb=1024kb     *     *  GB 吉字节     *  1GB=1024Mb     */    public static long getTotalSpace(File file){        long size=file.getTotalSpace();        return size/1024/1024;//getTotalSpace() 获取file指定路径的总空间    }    /**     * 获取file表示的可用空间     * @param file     * @return     */    public static long getFreeSpace(File file){        long size=file.getFreeSpace();        return size/1024/1024;//getFreeSpace() 获取file指定路径的可用空间    }    /**     * 根据file类的指定删除文件     * @param file     * @return     */    public static boolean deleteFiles(File file){        boolean bl=false;        if(file!=null && file.exists()){            bl=file.delete();        }        return bl;    }}
0 0