package com.moral.andbrickslib.utils; import android.app.Activity; import android.content.Context; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Environment; import android.provider.MediaStore; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; /** * 文件类 * author hjzhang */ public class FileUtils { public static boolean makeDirs(String filePath) { String folderName = getFolderName(filePath); if (StringUtils.isEmpty(folderName)) { return false; } File folder = new File(folderName); return (folder.exists() && folder.isDirectory()) ? true : folder.mkdirs(); } public static String getFolderName(String filePath) { if (StringUtils.isEmpty(filePath)) { return filePath; } int filePosi = filePath.lastIndexOf(File.separator); return (filePosi == -1) ? "" : filePath.substring(0, filePosi); } /** * @param filePath * @return * @see #makeDirs(String) */ public static boolean makeFolders(String filePath) { return makeDirs(filePath); } public static boolean makeFolders(File filePath) { return makeDirs(filePath.getAbsolutePath()); } /** * 文件是否存在 * * @param filePath * @return */ public static boolean exists(String filePath) { if (filePath == null || filePath.length() < 1) { return false; } File f = new File(filePath); if (!f.exists()) { return false; } return true; } /** * 创建文件路径 * * @param filePath 文件路径 * @return 成功true,失败false */ public static boolean mkdirs(String filePath) { if (null == filePath) { return false; } File file = new File(filePath); if (file.exists()) { return true; } return file.mkdirs(); } /** * 删除文件夹 * * @param filePath 文件夹路径 * @return */ public static boolean deleteDir(String filePath) { if (null == filePath) { return false; } File file = new File(filePath); if (file == null || !file.exists()) { return false; } if (file.isDirectory()) { File[] list = file.listFiles(); for (int i = 0; i < list.length; i++) { if (list[i].isDirectory()) { deleteDir(list[i].getAbsolutePath()); } else { list[i].delete(); } } } file.delete(); return true; } /** * 获取SD存储路径 * * @return 路径 */ public static String getSDFilePath() { return Environment.getExternalStorageDirectory().getAbsolutePath() + "/";// filePath:/sdcard/ } /** * 获取内部存储路径 * * @return 路径 */ public static String getRootFilePath() { return Environment.getDataDirectory().getAbsolutePath() + "/data/" + "com.ps.app" + "/"; // filePath: /data/data/ } /** * 获取存储主路径 * * @return */ public static String getFilePath() { if (hasSDCard()) { return getSDFilePath(); } return getRootFilePath(); } /** * 判断是否有SD卡 * * @return 是true ,否false */ public static boolean hasSDCard() { String status = Environment.getExternalStorageState(); if (!status.equals(Environment.MEDIA_MOUNTED)) { return false; } return true; } /** * 获取图片保存路径 * * @return */ public static String getImgPath() { String ImgPath = null; ImgPath = getFilePath() + "ps/picture/"; if (!exists(ImgPath)) { mkdirs(ImgPath); } return ImgPath; } /** * 获取录音保存路径 * * @return */ public static String getVoicePath() { String voicePath = null; voicePath = getFilePath() + "ps/voice/"; if (!exists(voicePath)) { mkdirs(voicePath); } return voicePath; } /** * 获取字符串 保存路径 * * @return */ public static String getStringPath() { String strPath = null; strPath = getFilePath() + "ps/str/"; if (!exists(strPath)) { mkdirs(strPath); } return strPath; } /** * 读取图片文件转成Bitmap * * @param path * @return */ public static Bitmap getBitmapFromFile(String path) { Bitmap b = null; b = BitmapFactory.decodeFile(path); return b; } /** * 将String数据存为文件 */ public static File getFileFromBytes(String name, String path) { byte[] b = name.getBytes(); BufferedOutputStream stream = null; File file = null; try { file = new File(path); FileOutputStream fstream = new FileOutputStream(file); stream = new BufferedOutputStream(fstream); stream.write(b); } catch (Exception e) { e.printStackTrace(); } finally { if (stream != null) { try { stream.close(); } catch (IOException e1) { e1.printStackTrace(); } } } return file; } /** * 将本地相册的Uri转成绝对路径 * * @param activity 上下文 * @param uri 本地相册图片Uri * @return 图片所在的文件路径 */ public static String UriToPath(Activity activity, Uri uri) { String[] proj = {MediaStore.Images.Media.DATA}; Cursor actualimagecursor = activity.managedQuery(uri, proj, null, null, null); int actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); actualimagecursor.moveToFirst(); String path = actualimagecursor.getString(actual_image_column_index); return path; } /** * 关闭流 * * @param closeables */ public static void closeIO(Closeable... closeables) { if (null == closeables || closeables.length <= 0) { return; } for (Closeable cb : closeables) { try { if (null == cb) { continue; } cb.close(); } catch (IOException e) { throw new RuntimeException( FileUtils.class.getClass().getName(), e); } } } /** * 从文件中读取文本 * * @param filePath * @return */ public static String readFile(String filePath) { InputStream is = null; try { is = new FileInputStream(filePath); } catch (Exception e) { throw new RuntimeException(FileUtils.class.getName() + "readFile---->" + filePath + " not found"); } return inputStream2String(is); } /** * 从assets中读取文本 * * @param name * @return */ public static String readFileFromAssets(Context context, String name) { InputStream is = null; try { is = context.getResources().getAssets().open(name); } catch (Exception e) { throw new RuntimeException(FileUtils.class.getName() + ".readFileFromAssets---->" + name + " not found"); } return inputStream2String(is); } /** * 输入流转字符串 * * @param is * @return 一个流中的字符串 */ public static String inputStream2String(InputStream is) { if (null == is) { return null; } StringBuilder resultSb = null; try { BufferedReader br = new BufferedReader(new InputStreamReader(is)); resultSb = new StringBuilder(); String len; while (null != (len = br.readLine())) { resultSb.append(len); } } catch (Exception ex) { } finally { closeIO(is); } return null == resultSb ? null : resultSb.toString(); } }