Qt Android image picker issue with Android > 5.5.1
Unsolved
Mobile and Embedded
-
Hi everyone,
I've tried to use this code (see the accepted answer) to create an image picker for Android in Qt (5.6):
http://stackoverflow.com/questions/34006608/qt-and-android-get-path-from-image-in-gallery/It works perfectly on Android < 5.1.1. In newer versions (also Android 6) the returned string is always empty.
they suggest a workaround in java but I've some problems in translating it to qt c++ code.
In particular I need this method:public static Uri handleImageUri(Uri uri) { Pattern pattern = Pattern.compile("(content://media/.*\\d)"); if (uri.getPath().contains("content")) { Matcher matcher = pattern.matcher(uri.getPath()); if (matcher.find()) return Uri.parse(matcher.group(1)); else throw new IllegalArgumentException("Cannot handle this URI"); } else return uri; }
Can someone help me?
Thank you. -
Hi, at the moment I'm using Qt c++ code and it works.
Here's the example:
void AndroidImagePicker::openPicker() { QAndroidJniObject Intent__ACTION_PICK = QAndroidJniObject::getStaticObjectField("android/content/Intent", "ACTION_PICK", "Ljava/lang/String;"); qDebug() << "AndroidImagePicker Intent__ACTION_PICK.isValid()=" << Intent__ACTION_PICK.isValid(); QAndroidJniObject EXTERNAL_CONTENT_URI= QAndroidJniObject::getStaticObjectField("android/provider/MediaStore$Images$Media", "EXTERNAL_CONTENT_URI", "Landroid/net/Uri;"); qDebug() << "AndroidImagePicker EXTERNAL_CONTENT_URI.isValid()=" << EXTERNAL_CONTENT_URI.isValid(); QAndroidJniObject intent=QAndroidJniObject("android/content/Intent", "(Ljava/lang/String;Landroid/net/Uri;)V", Intent__ACTION_PICK.object<jstring>(), EXTERNAL_CONTENT_URI.object<jobject>() ); qDebug() << "AndroidImagePicker intent.isValid()=" << intent.isValid(); QtAndroid::startActivity(intent, 101, this); } void AndroidImagePicker::handleActivityResult(int receiverRequestCode, int resultCode, const QAndroidJniObject &data) { jint RESULT_OK = QAndroidJniObject::getStaticField<jint>("android/app/Activity", "RESULT_OK"); if( receiverRequestCode == 101 && resultCode == RESULT_OK ) { QAndroidJniObject uri = data.callObjectMethod("getData", "()Landroid/net/Uri;"); qDebug("AndroidImagePicker uri = %s", qPrintable(uri.toString())); QAndroidJniObject dadosAndroid = QAndroidJniObject::getStaticObjectField("android/provider/MediaStore$MediaColumns", "DATA", "Ljava/lang/String;"); QAndroidJniEnvironment env; jobjectArray projecao = (jobjectArray)env->NewObjectArray(1, env->FindClass("java/lang/String"), NULL); jobject projacaoDadosAndroid = env->NewStringUTF(dadosAndroid.toString().toStdString().c_str()); env->SetObjectArrayElement(projecao, 0, projacaoDadosAndroid); QAndroidJniObject contentResolver = QtAndroid::androidActivity().callObjectMethod("getContentResolver", "()Landroid/content/ContentResolver;"); QAndroidJniObject nullObj; QAndroidJniObject cursor = contentResolver.callObjectMethod("query", "(Landroid/net/Uri;[Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;)Landroid/database/Cursor;", uri.object<jobject>(), projecao, nullObj.object<jstring>(), nullObj.object<jobjectArray>(), nullObj.object<jstring>()); qDebug() << "AndroidImagePicker cursor.isValid()=" << cursor.isValid(); jint columnIndex = cursor.callMethod<jint>("getColumnIndexOrThrow","(Ljava/lang/String;)I", dadosAndroid.object<jstring>()); qDebug() << "AndroidImagePicker column_index=" << columnIndex; cursor.callMethod<jboolean>("moveToFirst"); QAndroidJniObject path = cursor.callObjectMethod("getString", "(I)Ljava/lang/String;", columnIndex); qDebug() << "AndroidImagePicker path.isValid()=" << path.isValid(); QString imagePath = "file://" + path.toString(); qDebug() << "AndroidImagePicker path" << imagePath; cursor.callMethod<jboolean>("close"); emit imagePathRetrieved(imagePath); } else qWarning() << "AndroidImagePicker wrong path"; }
#ifndef ANDROIDIMAGEPICKER_H #define ANDROIDIMAGEPICKER_H #include <QObject> #include <QtAndroidExtras> #include <QDebug> class AndroidImagePicker : public QObject, public QAndroidActivityResultReceiver { Q_OBJECT public: AndroidImagePicker(); void openPicker(); virtual void handleActivityResult(int receiverRequestCode, int resultCode, const QAndroidJniObject &data); signals: void imagePathRetrieved(QString imagePath); }; #endif // ANDROIDIMAGEPICKER_H