Android文件操作(SD卡、缓存)工具类

来源:互联网 发布:软件可行性报告如何写 编辑:程序博客网 时间:2024/05/13 23:43
package com.qf.service_demo05.utils;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import android.annotation.SuppressLint;import android.content.Context;import android.graphics.Bitmap;import android.os.Build;import android.os.Environment;import android.os.StatFs;public class SDCardHelper {// 保存图片的目录// private static final String PATH =// Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)+File.separator;/** * @des 判断sd卡是否装载 *  * @return true:已经被装载可以使用 * */public static boolean isSDCardMounted() {return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);}/** * @des 获得sdcard的根目录 *  * @return sd卡根目录地址 * */public static String getSDCardBasePath() {if (isSDCardMounted()) {return Environment.getExternalStorageDirectory().getAbsolutePath();} else {return null;}}/** * @return 返回sd卡总共的容量 * */@SuppressLint("NewApi")public static long getSDCardTotalSize() {long size = 0;if (isSDCardMounted()) {StatFs statFs = new StatFs(getSDCardBasePath());if (Build.VERSION.SDK_INT >= 18) {size = statFs.getTotalBytes();} else {size = statFs.getBlockCount() * statFs.getBlockSize();}return size / 1024 / 1024;}return 0;}/** * @return 返回sd卡可用的容量 * */@SuppressLint("NewApi")public static long getSDCardAvailableSize() {long size = 0;if (isSDCardMounted()) {StatFs statFs = new StatFs(getSDCardBasePath());if (Build.VERSION.SDK_INT >= 18) {size = statFs.getAvailableBytes();} else {size = statFs.getAvailableBlocks() * statFs.getBlockSize();}return size / 1024 / 1024;}return 0;}/** * @return 返回sd卡空闲的容量 * */@SuppressLint("NewApi")public static long getSDCardFreeSize() {long size = 0;if (isSDCardMounted()) {StatFs statFs = new StatFs(getSDCardBasePath());if (Build.VERSION.SDK_INT >= 18) {size = statFs.getFreeBytes();} else {size = statFs.getFreeBlocks() * statFs.getBlockSize();}return size / 1024 / 1024;}return 0;}/** * @des 存储文件到sd卡的公共目录下 * @return true:表示存储成功 *  * @param data *            :代表存储的字节数组(任何文件都可以转化成字节数组) * @param type *            :代表sd卡公共目录的路径分类 * @param fileName *            :文件的名称 * */public static boolean saveFileToSDCardPublicDir(byte[] data, String type,String fileName) {if (isSDCardMounted()) {// 获得文件的公共目录File file = Environment.getExternalStoragePublicDirectory(type);FileOutputStream fos = null;BufferedOutputStream bos = null;try {fos = new FileOutputStream(new File(file, fileName));bos = new BufferedOutputStream(fos);bos.write(data, 0, data.length);bos.flush();return true;} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {fos.close();bos.close();} catch (IOException e) {e.printStackTrace();}}}return false;}/** * @des 保存文件到sd卡自定义目录之下 *  * @return true -->保存成功 * */public static boolean saveFileToSDCardCustomDir(byte[] data, String dir,String fileName) {if (isSDCardMounted()) {File file = new File(getSDCardBasePath() + File.separator + dir);// 判断这个文件夹是否存在,如果不存在就创建if (!file.exists()) {file.mkdirs();}FileOutputStream fos = null;BufferedOutputStream bos = null;try {fos = new FileOutputStream(new File(file, fileName));bos = new BufferedOutputStream(fos);bos.write(data, 0, data.length);bos.flush();return true;} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {fos.close();bos.close();} catch (IOException e) {e.printStackTrace();}}}return false;}/** * @des 保存文件到sd卡的私有目录之下 * */public static boolean saveFileToSDCardPrivateDir(byte[] data, String type,String fileName, Context context) {if (isSDCardMounted()) {BufferedOutputStream bos = null;// 获取当前应用程序在sd卡当中的私有目录File file = context.getExternalFilesDir(type);// 保存路径:/mnt/sdcard/Android/data/<你的包名>/filestry {bos = new BufferedOutputStream(new FileOutputStream(new File(file, fileName)));bos.write(data, 0, data.length);bos.flush();return true;} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {bos.close();} catch (IOException e) {e.printStackTrace();}}}return false;}/** * @des 保存文件到sd卡的缓存目录之下 * */public static boolean saveFileToSDCardCacheDir(byte[] data,String fileName, Context context) {if (isSDCardMounted()) {BufferedOutputStream bos = null;// 获取当前应用程序在sd卡当中的私有目录File file = context.getExternalCacheDir();// 保存路径:/mnt/sdcard/Android/data/<你的包名>/cache/....try {bos = new BufferedOutputStream(new FileOutputStream(new File(file, fileName)));bos.write(data, 0, data.length);bos.flush();return true;} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {bos.close();} catch (IOException e) {e.printStackTrace();}}}return false;}/** * @des 保存图片到sd卡的缓存目录之下 * */public static boolean saveBitmapToSDCardCacheDir(Bitmap bm,String fileName, Context context) {if (isSDCardMounted()) {BufferedOutputStream bos = null;// 获取当前应用程序在sd卡当中的缓存目录File file = context.getExternalCacheDir();// 保存路径:/mnt/sdcard/Android/data/<你的包名>/cache/....try {bos = new BufferedOutputStream(new FileOutputStream(new File(file, fileName)));if (fileName != null&& (fileName.contains(".png") || fileName.contains(".PNG"))) {/** * format:存储图片的格式 quality:比例 stream:写入的流 * */bm.compress(Bitmap.CompressFormat.PNG, 90, bos);} else {bm.compress(Bitmap.CompressFormat.JPEG, 90, bos);}bos.flush();return true;} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {bos.close();} catch (IOException e) {e.printStackTrace();}}}return false;}/** * @des 查找指定文件夹中的文件,并且把他读取出来 * */public static byte[] loadFileFromSDCard(String filePath) {BufferedInputStream bis = null;ByteArrayOutputStream baos = new ByteArrayOutputStream();File file = new File(filePath);if (file.exists()) {try {bis = new BufferedInputStream(new FileInputStream(file));int len = 0;byte[] buffer = new byte[1024 * 8];while ((len = bis.read(buffer)) != -1) {baos.write(buffer, 0, len);baos.flush();}return baos.toByteArray();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {bis.close();} catch (IOException e) {e.printStackTrace();}}}return null;}}


getCacheDir()方法用于获取/data/data/<application package>/cache目录
getFilesDir()方法用于获取/data/data/<application package>/files目录
应用程序在运行的过程中如果需要向手机上保存数据,一般是把数据保存在SDcard中的。
大部分应用是直接在SDCard的根目录下创建一个文件夹,然后把数据保存在该文件夹中。
这样当该应用被卸载后,这些数据还保留在SDCard中,留下了垃圾数据。
如果你想让你的应用被卸载后,与该应用相关的数据也清除掉,该怎么办呢?
通过Context.getExternalFilesDir()方法可以获取到 SDCard/Android/data/你的应用的包名/files/ 目录,
一般放一些长时间保存的数据
通过Context.getExternalCacheDir()方法可以获取到 SDCard/Android/data/你的应用包名/cache/目录,
一般存放临时缓存数据
如果使用上面的方法,当你的应用在被用户卸载后,SDCard/Android/data/你的应用的包名/ 这个目录下的所有文件都会被删除,
不会留下垃圾信息。
而且上面二个目录分别对应 设置->应用->应用详情里面的”清除数据“与”清除缓存“选项

如果要保存下载的内容,就不要放在以上目录下


0 0
原创粉丝点击