|
|
@ -374,6 +374,76 @@ public class FileUtil { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 检查内存空间是否充足 |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param path 文件路径 |
|
|
|
|
|
|
|
* @param fileSize 下载的文件的大小 |
|
|
|
|
|
|
|
* @return true 空间足够 |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public static boolean checkMemorySpace(String path, long fileSize) { |
|
|
|
|
|
|
|
File temp = new File(path); |
|
|
|
|
|
|
|
if (!temp.exists()){ |
|
|
|
|
|
|
|
if (!temp.getParentFile().exists()){ |
|
|
|
|
|
|
|
FileUtil.createDir(temp.getParentFile().getPath()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
path = temp.getParentFile().getPath(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
StatFs stat = new StatFs(path); |
|
|
|
|
|
|
|
long blockSize = stat.getBlockSize(); |
|
|
|
|
|
|
|
long availableBlocks = stat.getAvailableBlocks(); |
|
|
|
|
|
|
|
return fileSize <= availableBlocks * blockSize; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 读取下载配置文件 |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public static Properties loadConfig(File file) { |
|
|
|
|
|
|
|
Properties properties = new Properties(); |
|
|
|
|
|
|
|
FileInputStream fis = null; |
|
|
|
|
|
|
|
if (!file.exists()) { |
|
|
|
|
|
|
|
createFile(file.getPath()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
fis = new FileInputStream(file); |
|
|
|
|
|
|
|
properties.load(fis); |
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
|
|
} finally { |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
if (fis != null) { |
|
|
|
|
|
|
|
fis.close(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} catch (IOException e) { |
|
|
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return properties; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 保存配置文件 |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public static void saveConfig(File file, Properties properties) { |
|
|
|
|
|
|
|
FileOutputStream fos = null; |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
fos = new FileOutputStream(file, false); |
|
|
|
|
|
|
|
properties.store(fos, null); |
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
|
|
} finally { |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
if (fos != null) { |
|
|
|
|
|
|
|
fos.flush(); |
|
|
|
|
|
|
|
fos.close(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} catch (IOException e) { |
|
|
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 检查SD内存空间是否充足 |
|
|
|
* 检查SD内存空间是否充足 |
|
|
|
* |
|
|
|
* |
|
|
@ -381,7 +451,8 @@ public class FileUtil { |
|
|
|
* @param fileSize 文件大小 |
|
|
|
* @param fileSize 文件大小 |
|
|
|
* @return {@code false} 内存空间不足,{@code true}内存空间足够 |
|
|
|
* @return {@code false} 内存空间不足,{@code true}内存空间足够 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static boolean checkSDMemorySpace(String filePath, long fileSize) { |
|
|
|
@Deprecated |
|
|
|
|
|
|
|
private static boolean checkSDMemorySpace(String filePath, long fileSize) { |
|
|
|
List<String> dirs = FileUtil.getSDPathList(AriaConfig.getInstance().getAPP()); |
|
|
|
List<String> dirs = FileUtil.getSDPathList(AriaConfig.getInstance().getAPP()); |
|
|
|
if (dirs == null || dirs.isEmpty()) { |
|
|
|
if (dirs == null || dirs.isEmpty()) { |
|
|
|
return true; |
|
|
|
return true; |
|
|
@ -402,7 +473,7 @@ public class FileUtil { |
|
|
|
* @param sdcardPath sdcard 根路径 |
|
|
|
* @param sdcardPath sdcard 根路径 |
|
|
|
* @return 单位为:byte |
|
|
|
* @return 单位为:byte |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static long getAvailableExternalMemorySize(String sdcardPath) { |
|
|
|
private static long getAvailableExternalMemorySize(String sdcardPath) { |
|
|
|
StatFs stat = new StatFs(sdcardPath); |
|
|
|
StatFs stat = new StatFs(sdcardPath); |
|
|
|
long blockSize = stat.getBlockSize(); |
|
|
|
long blockSize = stat.getBlockSize(); |
|
|
|
long availableBlocks = stat.getAvailableBlocks(); |
|
|
|
long availableBlocks = stat.getAvailableBlocks(); |
|
|
@ -415,7 +486,7 @@ public class FileUtil { |
|
|
|
* @param sdcardPath sdcard 根路径 |
|
|
|
* @param sdcardPath sdcard 根路径 |
|
|
|
* @return 单位为:byte |
|
|
|
* @return 单位为:byte |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static long getTotalExternalMemorySize(String sdcardPath) { |
|
|
|
private static long getTotalExternalMemorySize(String sdcardPath) { |
|
|
|
StatFs stat = new StatFs(sdcardPath); |
|
|
|
StatFs stat = new StatFs(sdcardPath); |
|
|
|
long blockSize = stat.getBlockSize(); |
|
|
|
long blockSize = stat.getBlockSize(); |
|
|
|
long totalBlocks = stat.getBlockCount(); |
|
|
|
long totalBlocks = stat.getBlockCount(); |
|
|
@ -425,13 +496,11 @@ public class FileUtil { |
|
|
|
/** |
|
|
|
/** |
|
|
|
* 获取SD卡目录列表 |
|
|
|
* 获取SD卡目录列表 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static List<String> getSDPathList(Context context) { |
|
|
|
private static List<String> getSDPathList(Context context) { |
|
|
|
List<String> paths = null; |
|
|
|
List<String> paths = null; |
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { |
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
paths = getVolumeList(context); |
|
|
|
paths = getVolumeList(context); |
|
|
|
} catch (ClassNotFoundException e) { |
|
|
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
|
|
} catch (NoSuchMethodException e) { |
|
|
|
} catch (NoSuchMethodException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
e.printStackTrace(); |
|
|
|
} catch (IllegalAccessException e) { |
|
|
|
} catch (IllegalAccessException e) { |
|
|
@ -447,59 +516,11 @@ public class FileUtil { |
|
|
|
return paths; |
|
|
|
return paths; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 读取下载配置文件 |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public static Properties loadConfig(File file) { |
|
|
|
|
|
|
|
Properties properties = new Properties(); |
|
|
|
|
|
|
|
FileInputStream fis = null; |
|
|
|
|
|
|
|
if (!file.exists()) { |
|
|
|
|
|
|
|
createFile(file.getPath()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
fis = new FileInputStream(file); |
|
|
|
|
|
|
|
properties.load(fis); |
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
|
|
} finally { |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
if (fis != null) { |
|
|
|
|
|
|
|
fis.close(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} catch (IOException e) { |
|
|
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return properties; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 保存配置文件 |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public static void saveConfig(File file, Properties properties) { |
|
|
|
|
|
|
|
FileOutputStream fos = null; |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
fos = new FileOutputStream(file, false); |
|
|
|
|
|
|
|
properties.store(fos, null); |
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
|
|
} finally { |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
if (fos != null) { |
|
|
|
|
|
|
|
fos.flush(); |
|
|
|
|
|
|
|
fos.close(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} catch (IOException e) { |
|
|
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* getSDPathList |
|
|
|
* getSDPathList |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private static List<String> getVolumeList(final Context context) |
|
|
|
private static List<String> getVolumeList(final Context context) |
|
|
|
throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, |
|
|
|
throws NoSuchMethodException, InvocationTargetException, |
|
|
|
IllegalAccessException { |
|
|
|
IllegalAccessException { |
|
|
|
List<String> pathList = new ArrayList<>(); |
|
|
|
List<String> pathList = new ArrayList<>(); |
|
|
|
|
|
|
|
|
|
|
@ -589,7 +610,7 @@ public class FileUtil { |
|
|
|
* |
|
|
|
* |
|
|
|
* @return paths to all available SD-Cards in the system (include emulated) |
|
|
|
* @return paths to all available SD-Cards in the system (include emulated) |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static List<String> getStorageDirectories() { |
|
|
|
private static List<String> getStorageDirectories() { |
|
|
|
// Final set of paths
|
|
|
|
// Final set of paths
|
|
|
|
final List<String> rv = new ArrayList<>(); |
|
|
|
final List<String> rv = new ArrayList<>(); |
|
|
|
// Primary physical SD-CARD (not emulated)
|
|
|
|
// Primary physical SD-CARD (not emulated)
|
|
|
|