赞
踩
工作中用到的一些工具类,做一个笔记,以便日后回顾~
- 1,路径为:data/data/包名/app_+APP_CONFIG /APP_CONFIG ,其中包名后面的app_是为调用时,系统自己加上的
- Context context ;
- private final String APP_CONFIG ="config";
- File dirConf= context.getDir(APP_CONFIG, Context.MODE_PRIVATE);
- File conf = new File(dirConf, APP_CONFIG);
-
- 2,/data/data/<application package>/cache目录
- context.getCacheDir().getAbsolutePath();
-
- 3,/data/data/<application package>/files目录
- context.getFilesDir().getAbsolutePath();
-
- 4,取到 SDCard/Android/data/你的应用的包名/files/ 目录,一般放一些长时间保存的数据
- context.getExternalFilesDir().getAbsolutePath();
-
- 5,取到 SDCard/Android/data/你的应用包名/cache/目录,一般存放临时缓存数据
- context.getExternalCacheDir().getAbsolutePath();
-
- 6,/storage/sdcard/0这个储存路径需要申请权限
- Environment.getExternalStorageDirectory();
-
- 7,以第一个常量为例,音乐类别的公共目录绝对路径为:/storage/emulated/0/Music。
- 如果你使用文件管理器打开设备的外部存储空间的话,均可以看到这些公共目录文件夹。
-
- Environment.getExternalStoragePublicDirectory(String type);
- Envinonment 类提供诸多 type 参数的常量,比如:
-
- DIRECTORY_MUSIC:Music
- DIRECTORY_MOVIES:Movies
- DIRECTORY_PICTURES:Pictures
- DIRECTORY_DOWNLOADS:Download
- /**
- * 文件工具类
- *
- * @author mazhanzhu 2019年8月19日17:04:11
- */
- public class FileUtils {
- public static final int SIZETYPE_B = 1;//获取文件大小单位为B的double值
- public static final int SIZETYPE_KB = 2;//获取文件大小单位为KB的double值
- public static final int SIZETYPE_MB = 3;//获取文件大小单位为MB的double值
- public static final int SIZETYPE_GB = 4;//获取文件大小单位为GB的double值
- public static final String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
-
- /**
- * 格式转换文件大小
- *
- * @param context
- * @param fileSize
- * @return
- */
- @SuppressLint("NewApi")
- public static String formatFileSize(Context context, long fileSize) {
- return Formatter.formatFileSize(context, fileSize);
- }
-
- /**
- * 判断该文件是否存在
- *
- * @return
- */
- public static boolean isExist(String path) {
- try {
- File f = new File(path);
- if (f.exists()) {
- return true;
- }
- } catch (Exception e) {
- return false;
- }
- return false;
-
- }
-
- /**
- * 根据文件,读取里面的内容
- *
- * @param context 上下文
- * @param filename 文件的名字
- * @return 返回文件所包含的信息
- */
- public static String getLocalFile(Activity context, String filename) {
- String result = null;
- //内置sd卡路径
- File file = new File(Environment.getExternalStorageDirectory().getAbsoluteFile() + "/" + filename);
- Log.e("fd", "这是文件储存的路径: " + file.toString());
- if (file.exists()) {
- try {
- FileInputStream fis = new FileInputStream(file);
- // 把字节流转换为字节流
- BufferedReader br = new BufferedReader(new InputStreamReader(
- fis));
- result = br.readLine();
- } catch (Exception e) {
- e.printStackTrace();
- Log.e("fd", "文件读取错误: " + e.toString());
- }
- } else {
- ToastUtils.showToast("信息获取失败");
- context.finish();
- }
- return result;
- }
-
- /**
- * 判断手机是否安装某个应用
- *
- * @param context
- * @param appPackageName 应用包名
- * @return true:安装,false:未安装
- */
- public static boolean isApplicationAvilible(Context context, String appPackageName) {
- // 获取packagemanager
- PackageManager packageManager = context.getPackageManager();
- // 获取所有已安装程序的包信息
- List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
- if (pinfo != null) {
- for (int i = 0; i < pinfo.size(); i++) {
- String pn = pinfo.get(i).packageName;
- if (appPackageName.equals(pn)) {
- return true;
- }
- }
- }
- return false;
- }
-
- /**
- * 保存异常信息到文件
- *
- * @param data
- * @param file_name
- * @param isAppend 是否是追加, true为追加。 false为覆盖
- */
- public static void saveFile(String data, String file_name, boolean isAppend) {
- //内置sd卡路径
- File sdPath = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
- if (!sdPath.exists()) {
- sdPath.mkdirs();
- }
- Log.e("fd", "saveFile: " + sdPath.toString());
- File file = new File(sdPath, file_name);
- FileOutputStream fos = null;
- try {
- fos = new FileOutputStream(file, isAppend);
- fos.write(data.getBytes("UTF-8"));
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (fos != null) {
- try {
- fos.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
-
- /**
- * 获取文件指定文件的指定单位的大小
- *
- * @param filePath 文件路径
- * @param sizeType 获取大小的类型1为B、2为KB、3为MB、4为GB
- * @return double值的大小
- */
- public static double getFileOrFilesSize(String filePath, int sizeType) {
- File file = new File(filePath);
- long blockSize = 0;
- try {
- if (file.isDirectory()) {
- blockSize = getFileSizes(file);
- } else {
- blockSize = getFileSize(file);
- }
- } catch (Exception e) {
- e.printStackTrace();
- Log_Ma.e("获取文件大小", "获取失败!");
- }
- return FormetFileSize(blockSize, sizeType);
- }
-
- /**
- * 调用此方法自动计算指定文件或指定文件夹的大小
- *
- * @param filePath 文件路径
- * @return 计算好的带B、KB、MB、GB的字符串
- */
- public static String getAutoFileOrFilesSize(String filePath) {
- File file = new File(filePath);
- long blockSize = 0;
- try {
- if (file.isDirectory()) {
- blockSize = getFileSizes(file);
- } else {
- blockSize = getFileSize(file);
- }
- } catch (Exception e) {
- e.printStackTrace();
- Log_Ma.e("获取文件大小", "获取失败!");
- }
- return FormetFileSize(blockSize);
- }
-
- /**
- * 获取指定文件大小
- */
- public static long getFileSize(File file) {
- try {
- long size = 0;
- if (file.exists()) {
- FileInputStream fis = null;
- fis = new FileInputStream(file);
- size = fis.available();
- } else {
- file.createNewFile();
- Log_Ma.e("获取文件大小", "文件不存在!");
- }
- return size;
- } catch (Exception e) {
- return 0;
- }
- }
-
- /**
- * 获取指定文件夹
- *
- * @param f
- * @return
- * @throws Exception
- */
- public static long getFileSizes(File f) throws Exception {
- long size = 0;
- File flist[] = f.listFiles();
- for (int i = 0; i < flist.length; i++) {
- if (flist[i].isDirectory()) {
- size = size + getFileSizes(flist[i]);
- } else {
- size = size + getFileSize(flist[i]);
- }
- }
- return size;
- }
-
- /**
- * 将long类型的数据转成相应大小的字符串
- * <p>
- * (例:30.05MB、1.25GB、40.87KB)
- *
- * @param fileS 参数
- * @return 文件的大小描述
- */
- public static String FormetFileSize(long fileS) {
- DecimalFormat df = new DecimalFormat("0.00");
- String fileSizeString = "";
- String wrongSize = "0B";
- if (fileS == 0) {
- return wrongSize;
- }
- if (fileS < 1024) {
- fileSizeString = df.format((double) fileS) + "B";
- } else if (fileS < 1048576) {
- fileSizeString = df.format((double) fileS / 1024) + "KB";
- } else if (fileS < 1073741824) {
- fileSizeString = df.format((double) fileS / 1048576) + "MB";
- } else {
- fileSizeString = df.format((double) fileS / 1073741824) + "GB";
- }
- return fileSizeString;
- }
-
- /**
- * 转换文件大小,指定转换的类型
- *
- * @param fileS
- * @param sizeType
- * @return
- */
- public static double FormetFileSize(long fileS, int sizeType) {
- DecimalFormat df = new DecimalFormat("0.00");
- double fileSizeLong = 0;
- switch (sizeType) {
- case SIZETYPE_B:
- fileSizeLong = Double.valueOf(df.format((double) fileS));
- break;
- case SIZETYPE_KB:
- fileSizeLong = Double.valueOf(df.format((double) fileS / 1024));
- break;
- case SIZETYPE_MB:
- fileSizeLong = Double.valueOf(df.format((double) fileS / 1048576));
- break;
- case SIZETYPE_GB:
- fileSizeLong = Double.valueOf(df.format((double) fileS / 1073741824));
- break;
- default:
- break;
- }
- return fileSizeLong;
- }
-
- //android获取一个用于打开HTML文件的intent
- public static void Open_Html_File(Activity activity, File file) {
- Uri uri = Uri.parse(file.toString()).buildUpon().encodedAuthority("com.android.htmlfileprovider").scheme("content").encodedPath(file.toString()).build();
- Intent intent = new Intent(Intent.ACTION_VIEW);
- intent.setDataAndType(uri, "text/html");
- try {
- activity.startActivity(intent);
- } catch (Exception e) {
- ToastUtils.showToast("设备中没有安装支持该格式的程序");
- }
- }
-
-
- --------------------注意7.0以上要配置动态SD卡动态权限,不然不好使,详情参考博客里面FileProvider配置!!!!!-------------------------------------------------
-
- //android获取一个用于打开图片文件的intent
- public static void Open_Image_File(Activity activity, File file) {
- Intent intent = new Intent(Intent.ACTION_VIEW);
- intent.addCategory(Intent.CATEGORY_DEFAULT);
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- Uri uri = Uri.fromFile(file);
- intent.setDataAndType(uri, "image/*");
- try {
- activity.startActivity(intent);
- } catch (Exception e) {
- ToastUtils.showToast("设备中没有安装支持该格式的程序");
- }
- }
-
- //android获取一个用于打开PDF文件的intent
- public static void Open_PDF_File(Activity activity, File file) {
- Intent intent = new Intent(Intent.ACTION_VIEW);
- intent.addCategory(Intent.CATEGORY_DEFAULT);
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- Uri uri = Uri.fromFile(file);
- intent.setDataAndType(uri, "application/pdf");
- try {
- activity.startActivity(intent);
- } catch (Exception e) {
- ToastUtils.showToast("设备中没有安装支持该格式的程序");
- }
- }
-
- //android获取一个用于打开文本文件的intent
- public static void Open_Text_File(Activity activity, File file) {
- Intent intent = new Intent(Intent.ACTION_VIEW);
- intent.addCategory(Intent.CATEGORY_DEFAULT);
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- Uri uri = Uri.fromFile(file);
- intent.setDataAndType(uri, "text/plain");
- try {
- activity.startActivity(intent);
- } catch (Exception e) {
- ToastUtils.showToast("设备中没有安装支持该格式的程序");
- }
- }
-
- //android获取一个用于打开音频文件的intent
- public static void Open_Audio_File(Activity activity, File file) {
- Intent intent = new Intent(Intent.ACTION_VIEW);
- intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- intent.putExtra("oneshot", 0);
- intent.putExtra("configchange", 0);
- Uri uri = Uri.fromFile(file);
- intent.setDataAndType(uri, "audio/*");
- try {
- activity.startActivity(intent);
- } catch (Exception e) {
- ToastUtils.showToast("设备中没有安装支持该格式的程序");
- }
- }
-
- //android获取一个用于打开视频文件的intent
- public static void Open_Video_File(Activity activity,File file) {
- Intent intent = new Intent(Intent.ACTION_VIEW);
- intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- intent.putExtra("oneshot", 0);
- intent.putExtra("configchange", 0);
- Uri uri = Uri.fromFile(file);
- intent.setDataAndType(uri, "video/*");
- try {
- activity.startActivity(intent);
- } catch (Exception e) {
- ToastUtils.showToast("设备中没有安装支持该格式的程序");
- }
- }
-
-
- //android获取一个用于打开CHM文件的intent
- public static void Open_Chm_File(Activity activity, File file) {
- Intent intent = new Intent(Intent.ACTION_VIEW);
- intent.addCategory(Intent.CATEGORY_DEFAULT);
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- Uri uri = Uri.fromFile(file);
- intent.setDataAndType(uri, "application/x-chm");
- try {
- activity.startActivity(intent);
- } catch (Exception e) {
- ToastUtils.showToast("设备中没有安装支持该格式的程序");
- }
- }
-
-
- //android获取一个用于打开Word文件的intent
- public static void Open_Word_File(Activity activity, File file) {
- Intent intent = new Intent(Intent.ACTION_VIEW);
- intent.addCategory(Intent.CATEGORY_DEFAULT);
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- Uri uri = Uri.fromFile(file);
- intent.setDataAndType(uri, "application/msword");
- try {
- activity.startActivity(intent);
- } catch (Exception e) {
- ToastUtils.showToast("设备中没有安装支持该格式的程序");
- }
- }
-
- //android获取一个用于打开Excel文件的intent
- public static void Open_Excel_File(Activity activity, File file) {
- Intent intent = new Intent(Intent.ACTION_VIEW);
- intent.addCategory(Intent.CATEGORY_DEFAULT);
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- Uri uri = Uri.fromFile(file);
- intent.setDataAndType(uri, "application/vnd.ms-excel");
- try {
- activity.startActivity(intent);
- } catch (Exception e) {
- ToastUtils.showToast("设备中没有安装支持该格式的程序");
- }
- }
-
- //android获取一个用于打开PPT文件的intent
- public static void Open_PPT_File(Activity activity,File file) {
- Intent intent = new Intent(Intent.ACTION_VIEW);
- intent.addCategory(Intent.CATEGORY_DEFAULT);
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- Uri uri = Uri.fromFile(file);
- intent.setDataAndType(uri, "application/vnd.ms-powerpoint");
- try {
- activity.startActivity(intent);
- } catch (Exception e) {
- ToastUtils.showToast("设备中没有安装支持该格式的程序");
- }
- }
-
- //android获取一个用于打开apk文件的intent
- public static void Open_Apk_File(Activity activity, File file) {
- Intent intent = new Intent();
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- intent.setAction(android.content.Intent.ACTION_VIEW);
- intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
- try {
- activity.startActivity(intent);
- } catch (Exception e) {
- ToastUtils.showToast("设备中没有安装支持该格式的程序");
- }
- }
- /**
- * 获取公共目录
- *
- * @param type DIRECTORY_MUSIC 音乐类型
- * DIRECTORY_RINGTONES 铃声类型
- * DIRECTORY_PODCASTS 播客音频类型
- * DIRECTORY_ALARMS 闹钟提示音类型
- * DIRECTORY_NOTIFICATIONS 通知提示音类型
- * DIRECTORY_PICTURES 图片类型
- * DIRECTORY_MOVIES 电影类型
- * DIRECTORY_DOWNLOADS 下载文件类型
- * DIRECTORY_DCIM 相机照片类型
- * DIRECTORY_DOCUMENTS 文档类型
- * @return 相应类型目录文件
- */
- public File getExternalStoragePublicDirectory(String type) {
- File file = Environment.getExternalStoragePublicDirectory(type);
- if (!file.exists()) {
- file.mkdir();
- }
- return file;
- }
-
- public static String getTime(long time) {
- // SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
- Date date = new Date(time);
- String name = dateFormat.format(date);
- return name;
- }
-
- /**
- * 获取文件指定文件的指定单位的大小
- *
- * @param filePath 文件路径
- * @param sizeType 获取大小的类型1为B、2为KB、3为MB、4为GB
- * @return double值的大小
- */
- public static double getFileOrFilesSize(String filePath, int sizeType) {
- File file = new File(filePath);
- long blockSize = 0;
- try {
- if (file.isDirectory()) {
- blockSize = getFileSizes(file);
- } else {
- blockSize = getFileSize(file);
- }
- } catch (Exception e) {
- e.printStackTrace();
- Log_Ma.Companion.e("获取文件大小", "获取失败!");
- }
- return FormetFileSize(blockSize, sizeType);
- }
-
- /**
- * 获取指定文件夹
- */
- private static long getFileSizes(File f) throws Exception {
- long size = 0;
- File flist[] = f.listFiles();
- for (int i = 0; i < flist.length; i++) {
- if (flist[i].isDirectory()) {
- size = size + getFileSizes(flist[i]);
- } else {
- size = size + getFileSize(flist[i]);
- }
- }
- return size;
- }
-
- /**
- * 将long类型的数据转成相应大小的字符串
- * <p>
- * (例:30.05MB、1.25GB、40.87KB)
- *
- * @param fileS 参数
- * @return 文件的大小描述
- */
- private static String FormetFileSize(long fileS) {
- DecimalFormat df = new DecimalFormat("0.00");
- String fileSizeString = "";
- String wrongSize = "0B";
- if (fileS == 0) {
- return wrongSize;
- }
- if (fileS < 1024) {
- fileSizeString = df.format((double) fileS) + "B";
- } else if (fileS < 1048576) {
- fileSizeString = df.format((double) fileS / 1024) + "KB";
- } else if (fileS < 1073741824) {
- fileSizeString = df.format((double) fileS / 1048576) + "MB";
- } else {
- fileSizeString = df.format((double) fileS / 1073741824) + "GB";
- }
- return fileSizeString;
- }
-
- /**
- * 转换文件大小,指定转换的类型
- */
- private static double FormetFileSize(long fileS, int sizeType) {
- DecimalFormat df = new DecimalFormat("0.00");
- double fileSizeLong = 0;
- switch (sizeType) {
- case SIZETYPE_B:
- fileSizeLong = Double.valueOf(df.format((double) fileS));
- break;
- case SIZETYPE_KB:
- fileSizeLong = Double.valueOf(df.format((double) fileS / 1024));
- break;
- case SIZETYPE_MB:
- fileSizeLong = Double.valueOf(df.format((double) fileS / 1048576));
- break;
- case SIZETYPE_GB:
- fileSizeLong = Double.valueOf(df.format((double) fileS / 1073741824));
- break;
- default:
- break;
- }
- return fileSizeLong;
- }
-
- /**
- * 将指定的文件按着给定的文件的字节数进行分割文件
- *
- * @param filepath 源文件的路径
- * @param fileSize 指定的小文件的大小[MB] 0:默认等分4份
- */
- public static ArrayList<File> divide(String filepath, int fileSize) {
- ArrayList<File> fileArrayList = new ArrayList<>();
- if (!isExist(filepath)) {
- ToastUtils.showToast("指定文件不存在");
- } else {
- FileInputStream in = null;
- try {
- File file = new File(filepath);
- long fileLength = getFileSize(file);
- long size = fileSize * 1024 * 1024;// MB-->byte
- if (size <= 0) {
- size = fileLength / 4;
- }
- byte[] bytes = new byte[(int) size];
- // 取得被分割后的小文件的数目
- int num = (fileLength % size != 0) ? (int) (fileLength / size + 1) : (int) (fileLength / size);
- //输入文件流,即被分割的文件
- in = new FileInputStream(file);
- // 根据要分割的数目输出文件
- for (int i = 0; i < num; i++) {
- File outFile;
- try {
- //fixme 特殊分割字符需要添加\\
- String[] split = file.getName().split("\\.");
- if (split.length == 2) {
- outFile = new File(downpath, split[0] + "_" + i + ".part");
- } else {
- throw new Exception("");
- }
- } catch (Exception e) {
- outFile = new File(downpath, file.getName() + "_" + i + ".part");
- }
- if (outFile.exists()) {
- outFile.delete();
- }
- outFile.createNewFile();
- // 构建小文件的输出流
- FileOutputStream out = new FileOutputStream(outFile);
- out.write(bytes, 0, in.read(bytes));
- out.flush();
- out.close();
- fileArrayList.add(new File(outFile.getAbsolutePath()));
- }
- } catch (Exception e) {
- Log_Ma.Companion.e("Error[divide]:", e.toString());
- } finally {
- try {
- if (in != null) {
- in.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- return fileArrayList;
- }
-
- /**
- * @param filepath 源文件的路径
- * @param destDir 目标文件夹
- * @param fileSize 拆分大小【单位MB】
- * @throws Exception
- */
- public static ArrayList<File> split(String filepath, String destDir, int fileSize) {
- ArrayList<File> list = new ArrayList<>();
- RandomAccessFile rand = null;
- try {
- // 创建文件对象
- File src = new File(filepath);
- File destFolder = new File(destDir);
- if (!destFolder.exists()) {
- destFolder.mkdirs();
- }
- rand = new RandomAccessFile(src, "r");// 输入流
- // 计算拆分后文件的: 个数
- double size = fileSize * 1024 * 1024;// MB-->byte
- int count = (int) Math.ceil(src.length() / size);
-
- byte[] buf = new byte[(int) size];
- int len = 0;
- for (int i = 1; i <= count; i++) {
- // 定义拆分后的文件
- File destFile;
- try {
- //fixme 特殊分割字符需要添加\\
- String[] split = src.getName().split("\\.");
- destFile = new File(destDir, split[0] + "_" + i + "." + split[1]);
- } catch (Exception e) {
- destFile = new File(destDir, src.getName() + "_" + i + ".mp4");
- }
- destFile.createNewFile();// 创建空的目标文件
- RandomAccessFile dest = new RandomAccessFile(destFile, "rw");// 输出流
- // 拷贝文件
- len = rand.read(buf);
- dest.write(buf, 0, len);
- dest.close();
- list.add(destFile);
- }
- return list;
- } catch (Exception e) {
- Log_Ma.Companion.e("Error[split]:", e.toString());
- // 关闭资源
- if (rand != null) {
- try {
- rand.close();
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- }
- return list;
- }
- }
-
- //合并文件
- public static void merge(String srcDir, String destDir) throws Exception {
- // 多个输入流==>一个输出流
- File src = new File(srcDir);
- File[] files = src.listFiles();
- Arrays.sort(files, new Comparator<File>() {
-
- public int compare(File f1, File f2) {
- int index = f1.getName().lastIndexOf(".part");
- int num = Integer.parseInt(f1.getName().substring(index + 5));
- int index2 = f2.getName().lastIndexOf(".part");
- int num2 = Integer.parseInt(f2.getName().substring(index2 + 5));
-
- return num - num2;
- }
- });
- String formerName = "";
- for (File f : files) {
- if (f.isFile() && f.getName().lastIndexOf(".part") != -1) {
- formerName = f.getName().replaceAll(".part\\d", "");
- break;
- }
- }
- // 创建:一个输入流
- File formerFile = new File(destDir, formerName);
- System.out.println("原文件名===" + formerName);
-
- File parentFile = formerFile.getParentFile();
- if (!parentFile.exists()) {
- parentFile.mkdirs();// 创建父目录
- }
- if (!formerFile.exists()) {
- formerFile.createNewFile();
- }
- RandomAccessFile randTo = new RandomAccessFile(formerFile, "rw");// 输入流
-
- // 创建:多个输出流
- byte[] buf = new byte[1024];
- int len = 0;
- for (File f : files) {
- if (f.isFile() && f.getName().lastIndexOf(".part") != -1) {
- System.out.println("merge拆分的文件----" + f.getName());
- ;
- RandomAccessFile source = new RandomAccessFile(f, "r");// 输入流
- while ((len = source.read(buf)) != -1) {
- randTo.write(buf, 0, len);
- }
- source.close();// 关闭资源
- }
- }
- randTo.close();// 关闭资源
- System.out.println("merge ok...");
- }
- /**
- * 通知android媒体库更新文件夹
- *
- * @param filePath ilePath 文件绝对路径,、/sda/aaa/jjj.jpg
- */
- public static void scanFile(Context context, String filePath) {
- try {
- MediaScannerConnection.scanFile(context, new String[]{filePath}, null,
- new MediaScannerConnection.OnScanCompletedListener() {
- public void onScanCompleted(String path, Uri uri) {
- Log.i("*******", "Scanned " + path + ":");
- Log.i("*******", "-> uri=" + uri);
- }
- });
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * Uri转file文件
- */
- public static File getFile(Activity activity, Uri uri) {
- String[] arr = {MediaStore.Images.Media.DATA};
- Cursor cursor = activity.getContentResolver().query(uri, arr, null, null, null);
- int imgIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
- cursor.moveToFirst();
- String imgPath = cursor.getString(imgIndex);
- File file = new File(imgPath);
- return file;
- }
- }
- /**
- * Uri转file文件
- */
- public static File getFile(Activity activity, Uri uri) {
- String[] arr = {MediaStore.Images.Media.DATA};
- Cursor cursor = activity.getContentResolver().query(uri, arr, null, null, null);
- int imgIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
- cursor.moveToFirst();
- String imgPath = cursor.getString(imgIndex);
- File file = new File(imgPath);
- return file;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。