summaryrefslogtreecommitdiffstats
path: root/src/android
diff options
context:
space:
mode:
authorAndy Shaw <andy.shaw@qt.io>2020-02-04 11:39:29 +0100
committerAssam Boudjelthia <assam.boudjelthia@qt.io>2020-02-28 16:37:34 +0200
commit7e5f38aec667d3fd71efc0d2c586a08c3a87574c (patch)
treee076ca49e61b610326602c10d4197522d3900e21 /src/android
parentca9de962333085b1cf9a1eedf776b8b6b190adfd (diff)
Android: Add support for getting information about content uris
This enables things like size(), exists() to work with Android content uris with the provided uri given from a filedialog. It is expected that it is always a full path due to the nature of content uris, so relative paths will not work. Change-Id: I9c9ea42833677eb9d937b33e9dd42ee2a7d9c7c5 Reviewed-by: Assam Boudjelthia <assam.boudjelthia@qt.io> Reviewed-by: BogDan Vatra <bogdan@kdab.com>
Diffstat (limited to 'src/android')
-rw-r--r--src/android/jar/src/org/qtproject/qt5/android/QtNative.java57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/android/jar/src/org/qtproject/qt5/android/QtNative.java b/src/android/jar/src/org/qtproject/qt5/android/QtNative.java
index 87d326e225..a8bf4c15e1 100644
--- a/src/android/jar/src/org/qtproject/qt5/android/QtNative.java
+++ b/src/android/jar/src/org/qtproject/qt5/android/QtNative.java
@@ -44,6 +44,7 @@ import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.concurrent.Semaphore;
+import java.io.IOException;
import android.app.Activity;
import android.app.Service;
@@ -70,6 +71,7 @@ import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.InputDevice;
+import android.database.Cursor;
import java.lang.reflect.Method;
import java.security.KeyStore;
@@ -231,6 +233,61 @@ public class QtNative
}
}
+ public static long getSize(Context context, String contentUrl)
+ {
+ Uri uri = getUriWithValidPermission(context, contentUrl, "r");
+ long size = -1;
+
+ if (uri == null) {
+ Log.e(QtTAG, "getSize(): No permissions to open Uri");
+ return size;
+ }
+
+ try {
+ ContentResolver resolver = context.getContentResolver();
+ Cursor cur = resolver.query(uri, null, null, null, null);
+ if (cur != null) {
+ if (cur.moveToFirst())
+ size = cur.getLong(5); // size column
+ cur.close();
+ }
+ return size;
+ } catch (IllegalArgumentException e) {
+ Log.e(QtTAG, "getSize(): Invalid Uri");
+ return size;
+ } catch (UnsupportedOperationException e) {
+ Log.e(QtTAG, "getSize(): Unsupported operation for given Uri");
+ return size;
+ }
+ }
+
+ public static boolean checkFileExists(Context context, String contentUrl)
+ {
+ Uri uri = getUriWithValidPermission(context, contentUrl, "r");
+ boolean exists = false;
+
+ if (uri == null) {
+ Log.e(QtTAG, "checkFileExists(): No permissions to open Uri");
+ return exists;
+ }
+
+ try {
+ ContentResolver resolver = context.getContentResolver();
+ Cursor cur = resolver.query(uri, null, null, null, null);
+ if (cur != null) {
+ exists = true;
+ cur.close();
+ }
+ return exists;
+ } catch (IllegalArgumentException e) {
+ Log.e(QtTAG, "checkFileExists(): Invalid Uri");
+ return exists;
+ } catch (UnsupportedOperationException e) {
+ Log.e(QtTAG, "checkFileExists(): Unsupported operation for given Uri");
+ return false;
+ }
+ }
+
// this method loads full path libs
public static void loadQtLibraries(final ArrayList<String> libraries)
{