| ¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.doumee.keyCabinet.utils.update; |
| | | |
| | | import android.content.Context; |
| | | import android.graphics.Bitmap; |
| | | import android.os.Build; |
| | | import android.os.Environment; |
| | | import android.util.Log; |
| | | |
| | | import java.io.BufferedOutputStream; |
| | | import java.io.File; |
| | | import java.io.FileOutputStream; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.net.HttpURLConnection; |
| | | import java.net.URL; |
| | | import java.sql.Timestamp; |
| | | import java.text.DecimalFormat; |
| | | |
| | | /** |
| | | * æä»¶å·¥å
·ç±» |
| | | * |
| | | * @author spring sky |
| | | * |
| | | */ |
| | | public class FileUtil { |
| | | /** |
| | | * è·åç®å½åç§° |
| | | * |
| | | * @param url |
| | | * @return FileName |
| | | */ |
| | | public static String getFileName(String url) { |
| | | int lastIndexStart = url.lastIndexOf("/"); |
| | | if (lastIndexStart != -1) { |
| | | return url.substring(lastIndexStart + 1, url.length()); |
| | | } else { |
| | | return new Timestamp(System.currentTimeMillis()).toString(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * æä»¶æ¯å¦åå¨ |
| | | * |
| | | * @param path |
| | | * @return |
| | | */ |
| | | public static boolean FileIsExist(String path) { |
| | | File file = new File(path); |
| | | |
| | | return file.exists(); |
| | | } |
| | | |
| | | /** |
| | | * 夿SD塿¯å¦åå¨ |
| | | * |
| | | * @return boolean |
| | | */ |
| | | public static boolean checkSDCard() { |
| | | if (Environment.getExternalStorageState().equals( |
| | | Environment.MEDIA_MOUNTED)) { |
| | | return true; |
| | | } else { |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * ä¿åæä»¶å°æå®ç®å½ |
| | | * |
| | | * @param context |
| | | * @return æä»¶ä¿åçç®å½ |
| | | */ |
| | | public static String setMkdir(Context context) { |
| | | String filePath = null; |
| | | if (checkSDCard()) { |
| | | filePath = Environment.getExternalStorageDirectory() |
| | | + File.separator + "com.ahrykj.Weddingg" + File.separator |
| | | + "downloads"; |
| | | } else { |
| | | filePath = context.getCacheDir().getAbsolutePath() + File.separator |
| | | + "com.ahrykj.Weddingg" + File.separator + "downloads"; |
| | | } |
| | | File file = new File(filePath); |
| | | if (!file.exists()) { |
| | | file.mkdirs(); |
| | | Log.e("file", "ç®å½ä¸åå¨ å建ç®å½ "); |
| | | } else { |
| | | Log.e("file", "ç®å½åå¨"); |
| | | } |
| | | return filePath; |
| | | } |
| | | |
| | | public static String getFile(Context context){ |
| | | File sdDir = null; |
| | | boolean sdCardExist = Environment.getExternalStorageState().equals( |
| | | Environment.MEDIA_MOUNTED);// 夿sd塿¯å¦åå¨ |
| | | if (sdCardExist) { |
| | | if (Build.VERSION.SDK_INT>=29){ |
| | | //Android10ä¹å |
| | | sdDir = context.getExternalFilesDir(null); |
| | | }else { |
| | | sdDir = Environment.getExternalStorageDirectory();// è·åSD塿 ¹ç®å½ |
| | | } |
| | | } else { |
| | | sdDir = Environment.getRootDirectory();// è·åè·ç®å½ |
| | | } |
| | | return sdDir.toString(); |
| | | } |
| | | |
| | | /** |
| | | * è·åè·¯å¾ |
| | | * |
| | | * @return |
| | | * @throws IOException |
| | | */ |
| | | public static String getPath(Context context, String url) { |
| | | String path = null; |
| | | try { |
| | | path = FileUtil.getFile(context) + File.separator |
| | | + "ticketmachine.apk"; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return path; |
| | | } |
| | | |
| | | public static long getFileSize(File f) throws Exception// åå¾æä»¶å¤¹å¤§å° |
| | | { |
| | | long size = 0; |
| | | File flist[] = f.listFiles(); |
| | | if (flist != null) { |
| | | for (int i = 0; i < flist.length; i++) { |
| | | if (flist[i].isDirectory()) { |
| | | size = size + getFileSize(flist[i]); |
| | | } else { |
| | | size = size + flist[i].length(); |
| | | } |
| | | } |
| | | } |
| | | return size; |
| | | } |
| | | |
| | | /** |
| | | * è·åæä»¶çå¤§å° |
| | | * |
| | | * @param fileS |
| | | * æä»¶çå¤§å° |
| | | * @return |
| | | */ |
| | | public static String FormetFileSize(long fileS) {// è½¬æ¢æä»¶å¤§å° |
| | | DecimalFormat df = new DecimalFormat("0.0");// #.00 |
| | | String fileSizeString = ""; |
| | | if (fileS < 1024) { |
| | | if (fileS < 70) { |
| | | fileSizeString = "0B"; |
| | | } else { |
| | | fileSizeString = df.format((double) fileS) + "B"; |
| | | } |
| | | } else if (fileS < 1048576) { |
| | | fileSizeString = df.format((double) fileS / 1024) + "K"; |
| | | } else if (fileS < 1073741824) { |
| | | fileSizeString = df.format((double) fileS / 1048576) + "M"; |
| | | } else { |
| | | fileSizeString = df.format((double) fileS / 1073741824) + "G"; |
| | | } |
| | | return fileSizeString; |
| | | } |
| | | |
| | | /** |
| | | * å é¤è¯¥ç®å½ä¸çæå®æä»¶ |
| | | * |
| | | * @param filePath |
| | | * æä»¶ç®å½ |
| | | * @param fileName |
| | | * æä»¶å |
| | | * @return |
| | | */ |
| | | public static boolean deleteFile(String filePath, String fileName) { |
| | | if (FileIsExist(filePath + fileName)) { |
| | | File file = new File(filePath + fileName); |
| | | |
| | | return file.delete(); |
| | | } |
| | | |
| | | return true; |
| | | } |
| | | |
| | | /** |
| | | * å é¤è¯¥è·¯å¾æå®çæä»¶ |
| | | * |
| | | * @param path |
| | | * æä»¶è·¯å¾ |
| | | * @return |
| | | */ |
| | | public static boolean deleteFile(String path) { |
| | | if (FileIsExist(path)) { |
| | | File file = new File(path); |
| | | return file.delete(); |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | /** |
| | | * å 餿¹æ³ è¿éåªä¼å é¤æä¸ªæä»¶å¤¹ä¸çæä»¶ï¼å¦æä¼ å
¥çdirectoryæ¯ä¸ªæä»¶ï¼å°ä¸åå¤ç |
| | | * |
| | | * @param file |
| | | */ |
| | | |
| | | public static void deleteFilesByDirectory(File file) { |
| | | if (file.isFile()) { |
| | | file.delete(); |
| | | return; |
| | | } |
| | | |
| | | if (file.isDirectory()) { |
| | | File[] childFiles = file.listFiles(); |
| | | if (childFiles == null || childFiles.length == 0) { |
| | | file.delete(); |
| | | return; |
| | | } |
| | | |
| | | for (int i = 0; i < childFiles.length; i++) { |
| | | deleteFilesByDirectory(childFiles[i]); |
| | | } |
| | | file.delete(); |
| | | } |
| | | } |
| | | |
| | | //å°Bitmapè½¬æ¢æFile |
| | | public static File saveBitmapFile(Bitmap bitmap){ |
| | | File file=new File("/sdcard/face.jpg");//å°è¦ä¿åå¾ççè·¯å¾ |
| | | try { |
| | | BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file)); |
| | | bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos); |
| | | bos.flush(); |
| | | bos.close(); |
| | | } catch (IOException e) { |
| | | return null; |
| | | } |
| | | return file; |
| | | } |
| | | |
| | | public static interface DownLoadCallBack{ |
| | | void sucess(); |
| | | void err(String e); |
| | | } |
| | | |
| | | public static void downLoadFile(String filePath,String url,DownLoadCallBack callBack){ |
| | | try { |
| | | URL u = new URL(url); |
| | | HttpURLConnection conn = (HttpURLConnection) u.openConnection(); |
| | | conn.setConnectTimeout(10000); |
| | | int responeCode = conn.getResponseCode(); |
| | | InputStream is; |
| | | if (responeCode == 200) { |
| | | is = conn.getInputStream(); |
| | | } else { |
| | | //è¿æ¥å¤±è´¥ |
| | | if(callBack!=null){ |
| | | callBack.err("ä¸è½½å°åé误"); |
| | | } |
| | | return; |
| | | } |
| | | |
| | | int fileSize = conn.getContentLength(); |
| | | if (fileSize < 1 || is == null) { |
| | | //æä»¶å¤§å°ä¸å¯¹ |
| | | if(callBack!=null){ |
| | | callBack.err("æä»¶å¤§å°ä¸å¯¹"); |
| | | } |
| | | return; |
| | | } else { |
| | | //ä¸è½½ |
| | | if (FileUtil.deleteFile(filePath)) {// å é¤åæ¥çå®è£
æä»¶åä¸è½½ |
| | | FileOutputStream fos = new FileOutputStream(filePath); |
| | | byte[] bytes = new byte[1024]; |
| | | int len = -1; |
| | | while ((len = is.read(bytes)) != -1) { |
| | | fos.write(bytes, 0, len); |
| | | fos.flush(); |
| | | } |
| | | //ä¸è½½å®æ |
| | | if(callBack!=null){ |
| | | callBack.sucess(); |
| | | } |
| | | is.close(); |
| | | fos.close(); |
| | | } |
| | | } |
| | | } catch (Exception e) { |
| | | //ä¸è½½æ¥é |
| | | if(callBack!=null){ |
| | | callBack.err(e.getMessage()); |
| | | } |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |