Skip to content

Commit b35edbd

Browse files
Added file size filtering support
1 parent b9ac55f commit b35edbd

8 files changed

Lines changed: 376 additions & 6 deletions

File tree

Readme.md

Lines changed: 265 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,6 +1333,271 @@ properties.extensions = new String[]{"*"};
13331333
- Directories remain visible if readable, because users need to navigate through folders.
13341334
- If `selection_type` is `DIR_SELECT`, files are not selectable.
13351335

1336+
---
1337+
1338+
## File size filtering
1339+
1340+
FilePicker supports file size filtering using `min_file_size` and `max_file_size`.
1341+
1342+
This feature is not limited to video files. It works with **any file type** because the picker checks the actual file size in bytes.
1343+
1344+
You can use file size filtering for:
1345+
1346+
- Videos such as `mp4`, `mkv`, `avi`, `mov`
1347+
- Documents such as `pdf`, `doc`, `docx`, `xls`, `xlsx`
1348+
- Images such as `jpg`, `jpeg`, `png`, `webp`
1349+
- Audio files such as `mp3`, `wav`, `aac`
1350+
- Archives such as `zip`, `rar`, `7z`
1351+
- Any other readable file type
1352+
1353+
File size filtering can be used in three ways:
1354+
1355+
1. Allow files below a specific size.
1356+
2. Allow files above a specific size.
1357+
3. Allow files between a minimum and maximum size.
1358+
1359+
---
1360+
1361+
### File size values are in bytes
1362+
1363+
`min_file_size` and `max_file_size` accept values in **bytes**.
1364+
1365+
```java
1366+
properties.min_file_size = 5L * 1024L * 1024L; // 5 MB
1367+
properties.max_file_size = 50L * 1024L * 1024L; // 50 MB
1368+
```
1369+
1370+
The `L` is important because file sizes can become large. It tells Java to calculate the value as a `long`.
1371+
1372+
---
1373+
1374+
### Disable file size filtering
1375+
1376+
By default, file size filtering is disabled.
1377+
1378+
```java
1379+
properties.min_file_size = -1L;
1380+
properties.max_file_size = -1L;
1381+
```
1382+
1383+
`-1L` means there is no limit.
1384+
1385+
---
1386+
1387+
### Select any file below 50 MB
1388+
1389+
This example allows the user to select any readable file below 50 MB.
1390+
1391+
```java
1392+
DialogProperties properties = new DialogProperties();
1393+
1394+
properties.selection_mode = DialogConfigs.SINGLE_MODE;
1395+
properties.selection_type = DialogConfigs.FILE_SELECT;
1396+
1397+
// null means all file extensions are allowed
1398+
properties.extensions = null;
1399+
1400+
// Allow files up to 50 MB
1401+
properties.max_file_size = 50L * 1024L * 1024L;
1402+
1403+
// No minimum size limit
1404+
properties.min_file_size = -1L;
1405+
1406+
FilePickerDialog dialog = new FilePickerDialog(MainActivity.this, properties);
1407+
dialog.setTitle("Select File Below 50 MB");
1408+
dialog.show();
1409+
```
1410+
1411+
---
1412+
1413+
### Select video files below 100 MB
1414+
1415+
This example allows only video files below 100 MB.
1416+
1417+
```java
1418+
DialogProperties properties = new DialogProperties();
1419+
1420+
properties.selection_mode = DialogConfigs.SINGLE_MODE;
1421+
properties.selection_type = DialogConfigs.FILE_SELECT;
1422+
1423+
// Only video files
1424+
properties.extensions = new String[]{
1425+
"mp4",
1426+
"mkv",
1427+
"avi",
1428+
"mov",
1429+
"webm",
1430+
"3gp"
1431+
};
1432+
1433+
// Allow videos up to 100 MB
1434+
properties.max_file_size = 100L * 1024L * 1024L;
1435+
1436+
// No minimum size limit
1437+
properties.min_file_size = -1L;
1438+
1439+
FilePickerDialog dialog = new FilePickerDialog(MainActivity.this, properties);
1440+
dialog.setTitle("Select Video Below 100 MB");
1441+
dialog.show();
1442+
```
1443+
1444+
---
1445+
1446+
### Select PDF files below 10 MB
1447+
1448+
```java
1449+
DialogProperties properties = new DialogProperties();
1450+
1451+
properties.selection_mode = DialogConfigs.SINGLE_MODE;
1452+
properties.selection_type = DialogConfigs.FILE_SELECT;
1453+
1454+
// Only PDF files
1455+
properties.extensions = new String[]{"pdf"};
1456+
1457+
// Allow PDF files up to 10 MB
1458+
properties.max_file_size = 10L * 1024L * 1024L;
1459+
1460+
FilePickerDialog dialog = new FilePickerDialog(MainActivity.this, properties);
1461+
dialog.setTitle("Select PDF Below 10 MB");
1462+
dialog.show();
1463+
```
1464+
1465+
---
1466+
1467+
### Select files larger than 20 MB
1468+
1469+
You can also allow only files larger than a specific size by using `min_file_size`.
1470+
1471+
```java
1472+
DialogProperties properties = new DialogProperties();
1473+
1474+
properties.selection_mode = DialogConfigs.SINGLE_MODE;
1475+
properties.selection_type = DialogConfigs.FILE_SELECT;
1476+
1477+
// Allow all file types
1478+
properties.extensions = null;
1479+
1480+
// Only show files larger than 20 MB
1481+
properties.min_file_size = 20L * 1024L * 1024L;
1482+
1483+
// No maximum size limit
1484+
properties.max_file_size = -1L;
1485+
1486+
FilePickerDialog dialog = new FilePickerDialog(MainActivity.this, properties);
1487+
dialog.setTitle("Select File Larger Than 20 MB");
1488+
dialog.show();
1489+
```
1490+
1491+
---
1492+
1493+
### Select files between 5 MB and 200 MB
1494+
1495+
This example allows any file type between 5 MB and 200 MB.
1496+
1497+
```java
1498+
DialogProperties properties = new DialogProperties();
1499+
1500+
properties.selection_mode = DialogConfigs.SINGLE_MODE;
1501+
properties.selection_type = DialogConfigs.FILE_SELECT;
1502+
1503+
// Allow all file types
1504+
properties.extensions = null;
1505+
1506+
// Minimum size: 5 MB
1507+
properties.min_file_size = 5L * 1024L * 1024L;
1508+
1509+
// Maximum size: 200 MB
1510+
properties.max_file_size = 200L * 1024L * 1024L;
1511+
1512+
FilePickerDialog dialog = new FilePickerDialog(MainActivity.this, properties);
1513+
dialog.setTitle("Select File Between 5 MB and 200 MB");
1514+
dialog.show();
1515+
```
1516+
1517+
---
1518+
1519+
### Select large video files between 100 MB and 2 GB
1520+
1521+
FilePicker also supports large file size limits because `min_file_size` and `max_file_size` use `long`.
1522+
1523+
```java
1524+
DialogProperties properties = new DialogProperties();
1525+
1526+
properties.selection_mode = DialogConfigs.SINGLE_MODE;
1527+
properties.selection_type = DialogConfigs.FILE_SELECT;
1528+
1529+
// Only video files
1530+
properties.extensions = new String[]{
1531+
"mp4",
1532+
"mkv",
1533+
"avi",
1534+
"mov",
1535+
"webm"
1536+
};
1537+
1538+
// Minimum size: 100 MB
1539+
properties.min_file_size = 100L * 1024L * 1024L;
1540+
1541+
// Maximum size: 2 GB
1542+
properties.max_file_size = 2L * 1024L * 1024L * 1024L;
1543+
1544+
FilePickerDialog dialog = new FilePickerDialog(MainActivity.this, properties);
1545+
dialog.setTitle("Select Large Video File");
1546+
dialog.show();
1547+
```
1548+
1549+
---
1550+
1551+
### Helper method for converting MB to bytes
1552+
1553+
For cleaner code, you can create a helper method:
1554+
1555+
```java
1556+
private long mbToBytes(long mb) {
1557+
return mb * 1024L * 1024L;
1558+
}
1559+
```
1560+
1561+
Usage:
1562+
1563+
```java
1564+
properties.max_file_size = mbToBytes(50); // 50 MB
1565+
properties.min_file_size = mbToBytes(5); // 5 MB
1566+
```
1567+
1568+
---
1569+
1570+
### Helper method for converting GB to bytes
1571+
1572+
```java
1573+
private long gbToBytes(long gb) {
1574+
return gb * 1024L * 1024L * 1024L;
1575+
}
1576+
```
1577+
1578+
Usage:
1579+
1580+
```java
1581+
properties.max_file_size = gbToBytes(2); // 2 GB
1582+
```
1583+
1584+
---
1585+
1586+
### Important notes
1587+
1588+
- File size values must be provided in bytes.
1589+
- Use `L` in size calculations to avoid integer overflow.
1590+
- `-1L` means no limit.
1591+
- `min_file_size` checks the minimum allowed file size.
1592+
- `max_file_size` checks the maximum allowed file size.
1593+
- File size filtering works with all file types, not only videos.
1594+
- Extension filtering and file size filtering can be combined.
1595+
- Directories are not blocked by file size filters because users still need to browse folders.
1596+
- File size filtering only applies to files.
1597+
- If both `min_file_size` and `max_file_size` are set, the file must be inside that size range.
1598+
- If `min_file_size` is greater than `max_file_size`, no file may match your filter, so validate your values before showing the dialog.
1599+
- On Android 11 and above, storage access rules still apply. File size filtering does not bypass Android scoped storage or Play Store permission policies.
1600+
13361601
---
13371602

13381603
## Single and multiple selection

library/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ group='com.github.TutorialsAndroid'
44

55
ext {
66
PUBLISH_GROUP_ID = 'io.github.tutorialsandroid'
7-
PUBLISH_VERSION = '10.1.1'
7+
PUBLISH_VERSION = '10.1.2'
88
PUBLISH_ARTIFACT_ID = 'filepicker'
99
PUBLISH_DESCRIPTION = 'Android Library to select files/directories from Device Storage'
1010
PUBLISH_URL = 'https://github.com/TutorialsAndroid/FilePicker'

library/src/main/java/com/developer/filepicker/model/DialogProperties.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,20 @@ public class DialogProperties {
2929
*/
3030
public boolean show_permission_error_toast;
3131

32+
/**
33+
* Maximum allowed file size in bytes.
34+
*
35+
* Default value -1 means no maximum size filter.
36+
*/
37+
public long max_file_size = -1L;
38+
39+
/**
40+
* Minimum allowed file size in bytes.
41+
*
42+
* Default value -1 means no minimum size filter.
43+
*/
44+
public long min_file_size = -1L;
45+
3246
public DialogProperties() {
3347
selection_mode = DialogConfigs.SINGLE_MODE;
3448
selection_type = DialogConfigs.FILE_SELECT;
@@ -37,7 +51,9 @@ public DialogProperties() {
3751
offset = new File(DialogConfigs.DEFAULT_DIR);
3852
extensions = null;
3953
show_hidden_files = false;
40-
allow_manage_external_storage = true;
41-
show_permission_error_toast = true;
54+
55+
// v10.1.2 file size filters
56+
max_file_size = -1L;
57+
min_file_size = -1L;
4258
}
4359
}

library/src/main/java/com/developer/filepicker/utils/ExtensionFilter.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ public boolean accept(File file) {
3838
return false;
3939
}
4040

41+
if (!isFileSizeAllowed(file)) {
42+
return false;
43+
}
44+
4145
if (validExtensions.length == 0) {
4246
return true;
4347
}
@@ -48,9 +52,24 @@ public boolean accept(File file) {
4852
return true;
4953
}
5054
}
55+
5156
return false;
5257
}
5358

59+
private boolean isFileSizeAllowed(File file) {
60+
long fileSize = file.length();
61+
62+
if (properties.min_file_size >= 0 && fileSize < properties.min_file_size) {
63+
return false;
64+
}
65+
66+
if (properties.max_file_size >= 0 && fileSize > properties.max_file_size) {
67+
return false;
68+
}
69+
70+
return true;
71+
}
72+
5473
private static String[] normalizeExtensions(String[] extensions) {
5574
if (extensions == null || extensions.length == 0) {
5675
return new String[0];

sample/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ android {
88
applicationId "com.developer.filepicker.file"
99
minSdkVersion 23
1010
targetSdkVersion 37
11-
versionCode 16
12-
versionName "10.1.1"
11+
versionCode 17
12+
versionName "10.1.2"
1313
}
1414
buildTypes {
1515
release {

0 commit comments

Comments
 (0)