5
5
import android .content .res .AssetFileDescriptor ;
6
6
import android .net .Uri ;
7
7
import android .os .Build ;
8
+ import android .os .StatFs ;
9
+ import android .text .TextUtils ;
8
10
9
11
import java .io .BufferedInputStream ;
10
12
import java .io .File ;
@@ -76,10 +78,10 @@ public static boolean isFileExists(final String filePath) {
76
78
if (file .exists ()) {
77
79
return true ;
78
80
}
79
- return isFileExists29 (filePath );
81
+ return isFileExistsApi29 (filePath );
80
82
}
81
83
82
- private static boolean isFileExists29 (String filePath ) {
84
+ private static boolean isFileExistsApi29 (String filePath ) {
83
85
if (Build .VERSION .SDK_INT >= 29 ) {
84
86
try {
85
87
Uri uri = Uri .parse (filePath );
@@ -1379,6 +1381,15 @@ public static String getFileExtension(final String filePath) {
1379
1381
return filePath .substring (lastPoi + 1 );
1380
1382
}
1381
1383
1384
+ /**
1385
+ * Notify system to scan the file.
1386
+ *
1387
+ * @param filePath The path of file.
1388
+ */
1389
+ public static void notifySystemToScan (final String filePath ) {
1390
+ notifySystemToScan (getFileByPath (filePath ));
1391
+ }
1392
+
1382
1393
/**
1383
1394
* Notify system to scan the file.
1384
1395
*
@@ -1393,12 +1404,45 @@ public static void notifySystemToScan(final File file) {
1393
1404
}
1394
1405
1395
1406
/**
1396
- * Notify system to scan the file.
1407
+ * Return the total size of file system .
1397
1408
*
1398
- * @param filePath The path of file.
1409
+ * @param anyPathInFs Any path in file system.
1410
+ * @return the total size of file system
1399
1411
*/
1400
- public static void notifySystemToScan (final String filePath ) {
1401
- notifySystemToScan (getFileByPath (filePath ));
1412
+ public static long getFsTotalSize (String anyPathInFs ) {
1413
+ if (TextUtils .isEmpty (anyPathInFs )) return 0 ;
1414
+ StatFs statFs = new StatFs (anyPathInFs );
1415
+ long blockSize ;
1416
+ long totalSize ;
1417
+ if (android .os .Build .VERSION .SDK_INT >= android .os .Build .VERSION_CODES .JELLY_BEAN_MR2 ) {
1418
+ blockSize = statFs .getBlockSizeLong ();
1419
+ totalSize = statFs .getBlockCountLong ();
1420
+ } else {
1421
+ blockSize = statFs .getBlockSize ();
1422
+ totalSize = statFs .getBlockCount ();
1423
+ }
1424
+ return blockSize * totalSize ;
1425
+ }
1426
+
1427
+ /**
1428
+ * Return the available size of file system.
1429
+ *
1430
+ * @param anyPathInFs Any path in file system.
1431
+ * @return the available size of file system
1432
+ */
1433
+ public static long getFsAvailableSize (final String anyPathInFs ) {
1434
+ if (TextUtils .isEmpty (anyPathInFs )) return 0 ;
1435
+ StatFs statFs = new StatFs (anyPathInFs );
1436
+ long blockSize ;
1437
+ long availableSize ;
1438
+ if (android .os .Build .VERSION .SDK_INT >= android .os .Build .VERSION_CODES .JELLY_BEAN_MR2 ) {
1439
+ blockSize = statFs .getBlockSizeLong ();
1440
+ availableSize = statFs .getAvailableBlocksLong ();
1441
+ } else {
1442
+ blockSize = statFs .getBlockSize ();
1443
+ availableSize = statFs .getAvailableBlocks ();
1444
+ }
1445
+ return blockSize * availableSize ;
1402
1446
}
1403
1447
1404
1448
///////////////////////////////////////////////////////////////////////////
0 commit comments