From e09e9f8a34cbc99a33dfa9ef1792b0025575c3a8 Mon Sep 17 00:00:00 2001 From: xuxiuxi <xuxiuxi@454eff88-639b-444f-9e54-f578c98de674> Date: 星期二, 01 八月 2017 11:41:19 +0800 Subject: [PATCH] --- VisitFace/DemoForBsk/app/src/main/java/cn/com/basic/face/util/FileUtil.java | 129 ++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 128 insertions(+), 1 deletions(-) diff --git a/VisitFace/DemoForBsk/app/src/main/java/cn/com/basic/face/util/FileUtil.java b/VisitFace/DemoForBsk/app/src/main/java/cn/com/basic/face/util/FileUtil.java index df47076..7055517 100644 --- a/VisitFace/DemoForBsk/app/src/main/java/cn/com/basic/face/util/FileUtil.java +++ b/VisitFace/DemoForBsk/app/src/main/java/cn/com/basic/face/util/FileUtil.java @@ -1,8 +1,17 @@ package cn.com.basic.face.util; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; + import java.io.BufferedOutputStream; +import java.io.BufferedReader; import java.io.File; +import java.io.FileInputStream; import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.RandomAccessFile; import cn.com.basic.face.base.MainActivity; @@ -11,6 +20,19 @@ public static File writeToFile(String fileName, byte[] fileBytes) { try { + return writeToFile(fileName, fileBytes, 0, fileBytes.length); + }catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + + public static File writeToFile(String fileName, byte[] fileBytes, int offset, int size) { + try { + if (fileBytes == null) { + fileBytes = new byte[]{}; + } String dir = MainActivity.getInstance().getFilesDir().getAbsolutePath(); File file = new File(dir, fileName); @@ -19,7 +41,7 @@ } file.createNewFile(); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(dir, fileName))); - bos.write(fileBytes); + bos.write(fileBytes, offset, size); bos.flush(); bos.close(); return new File(dir, fileName); @@ -29,4 +51,109 @@ return null; } + public static File getFile(String shortFileName) { + String dir = MainActivity.getInstance().getFilesDir().getAbsolutePath(); + return new File(dir, shortFileName); + } + + public static File getPhotoFile() { + File dir = new File(MainActivity.getInstance().getFilesDir().getAbsolutePath()); + File[] photoFiles = dir.listFiles(); + if (photoFiles != null) { + for (File file : photoFiles) { + if (file.isDirectory()) { + continue; + } + String name = file.getName(); + if (name.length() != "c248fe8f-6da5-4ec9-bffa-1a11bcf2fdc7".length()) { + continue; + } + if (file.isFile()) { + return file; + } + } + } + return null; + } + + + public static byte[] readFile(File file) { + // Open file + RandomAccessFile f = null; + try { + f = new RandomAccessFile(file, "r"); + // Get and check length + long longlength = f.length(); + int length = (int) longlength; + if (length != longlength) + throw new IOException("File size >= 2 GB"); + // Read file and return data + byte[] data = new byte[length]; + f.readFully(data); + return data; + } catch (Exception e){ + e.printStackTrace(); + } finally { + try { + if (f != null) { + f.close(); + } + }catch (Exception e) { + e.printStackTrace(); + } + } + return new byte[]{}; + } + + + public static void deleteFile(String fileName) { + try { + String dir = MainActivity.getInstance().getFilesDir().getAbsolutePath(); + + File file = new File(dir, fileName); + if (file.exists()) { + file.delete(); + } + }catch (Exception e) { + e.printStackTrace(); + } + } + + public static String convertStreamToString(InputStream is) throws Exception { + BufferedReader reader = new BufferedReader(new InputStreamReader(is)); + StringBuilder sb = new StringBuilder(); + String line = null; + while ((line = reader.readLine()) != null) { + sb.append(line); + } + reader.close(); + return sb.toString(); + } + + public static String getStringFromFile (String filePath) { + try { + File fl = new File(filePath); + FileInputStream fin = new FileInputStream(fl); + String ret = convertStreamToString(fin); + fin.close(); + return ret; + } catch (Exception e) { + e.printStackTrace(); + } + return ""; + } + + public static Bitmap loadBitmap(String fileName) { + String dir = MainActivity.getInstance().getFilesDir().getAbsolutePath(); + File file = new File(dir, fileName); + if (file.exists()) { + BitmapFactory.Options options = new BitmapFactory.Options(); + options.inPreferredConfig = Bitmap.Config.ARGB_8888; + Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), options); + return bitmap; + } else { + return null; + } + } + } -- Gitblit v1.8.0