aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/conversationsplus/utils/FileUtils.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/de/thedevstack/conversationsplus/utils/FileUtils.java')
-rw-r--r--src/main/java/de/thedevstack/conversationsplus/utils/FileUtils.java36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/main/java/de/thedevstack/conversationsplus/utils/FileUtils.java b/src/main/java/de/thedevstack/conversationsplus/utils/FileUtils.java
index d1ac2d34..3ee15f70 100644
--- a/src/main/java/de/thedevstack/conversationsplus/utils/FileUtils.java
+++ b/src/main/java/de/thedevstack/conversationsplus/utils/FileUtils.java
@@ -10,8 +10,10 @@ import android.os.Build;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
+import android.provider.OpenableColumns;
import java.io.File;
+import java.util.List;
import de.thedevstack.conversationsplus.ConversationsPlusApplication;
@@ -187,6 +189,40 @@ public final class FileUtils {
return secondToLastPart;
}
+ /**
+ * Retrieve file size from given uri
+ * @param context actual Context
+ * @param uri uri to file
+ * @return file size or -1 in case of error
+ */
+ private static long getFileSize(Context context, Uri uri) {
+ Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
+ if (cursor != null && cursor.moveToFirst()) {
+ return cursor.getLong(cursor.getColumnIndex(OpenableColumns.SIZE));
+ } else {
+ return -1;
+ }
+ }
+
+ /**
+ * Check for given list of uris if corresponding file sizes are all smaller than given maximum
+ * @param context actual Context
+ * @param uris list of uris
+ * @param max maximum file size
+ * @return true if all file sizes are smaller than max, false otherwise
+ */
+ public static boolean allFilesUnderSize(Context context, List<Uri> uris, long max) {
+ if (max <= 0) {
+ return true; //exception to be compatible with HTTP Upload < v0.2
+ }
+ for(Uri uri : uris) {
+ if (getFileSize(context, uri) > max) {
+ return false;
+ }
+ }
+ return true;
+ }
+
private FileUtils() {
// Utility class - do not instantiate
}