Android/ios image picker
-
wrote on 2 Sept 2014, 14:58 last edited by
Hello,
I am actively searching for a component or similar to be able to show picture within the smartphone and select one or more of those...
I have searched a lot but nothing I found.
Someone know a bit more or someone has already done something similar and want to share that?Thanks
Gianni -
Hi,
For iOS you can look at the QtDD 2013 iOS presentation code. There's the Objective-C code available.
Hope it helps
-
wrote on 3 Sept 2014, 09:50 last edited by
Hi,
thank you so this code should work under Qt...and for android Do you know something or not?
I didn't see any plan for this feature and it is very strange such lack in a mobile sdk! -
Yes it does. For Android, I haven't tested it but you would probably need to use JNI for the same thing (qandroidextras comes to mind)
There's only so much people working on Qt and there are priorities they have to follow. Anyway it's something that might appear in the extras repository in the future :)
-
wrote on 4 Sept 2014, 06:25 last edited by
Anyway I was not able to find ios code...
-
wrote on 4 Sept 2014, 19:07 last edited by
For iOS you need to use UIImagePickerController, and because it's very easy to mix Qt C++ code with Objective-C code, just look with google on a tutorial for using UIImagePickerController and you can directly use that code.
For Android the road it's a bit more difficult, this is my solution:
-
Create a custom android java Activity subclassing QtActivity, into the java class define some static method for constructing the necessary Intent:
@
public static Intent createChoosePhotoIntent() {
Intent intent = new Intent( Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI );
intent.setType("image/*");
return Intent.createChooser(intent, "Select Image");
}
@ -
Create a C++ subclass of QAndroidActivityResultReceiver for getting the result of the Intent launched (in this case the images chosen):
@
class OnSelectPhotoResult : public QAndroidActivityResultReceiver {
void handleActivityResult(int requestCode, int resultCode, const QAndroidJniObject & data) {
qDebug() << "TOTAPE RETURNING FROM SELECT PHOTO - " << resultCode << " - " << requestCode;
if ( resultCode == -1 ) {
// OK !
QAndroidJniObject imageUri = data.callObjectMethod(
"getData",
"()Landroid/net/Uri;");
QAndroidJniObject imageFile = QAndroidJniObject::callStaticObjectMethod(
"com/totape/app/TotapeActivity",
"getPath",
"(Landroid/net/Uri;)Ljava/lang/String;",
imageUri.object<jobject>());
Backend::instance()->photoReady( imageFile.toString() );
}
}
};
@
In my code above, I use java methods to process the result, this because the returned data are java object, so you need to call java methods via JNI in order to extract information. For doing so, personally I prefer to create static method in the custom Activity in order to minimize the number of JNI calls -
Launch the Intent from C++ with this code:
@
void Backend::selectPhoto() {
QAndroidJniObject intent = QAndroidJniObject::callStaticObjectMethod(
"com/totape/app/TotapeActivity",
"createChoosePhotoIntent",
"()Landroid/content/Intent;" );
qDebug() << "STARTING INTENT FOR SELECTING A PHOTO";
QtAndroid::startActivity( intent, 12051978, data->onSelectPhoto );
}
@
That's it.
This is my solution. -
-
wrote on 5 Sept 2014, 06:42 last edited by
Nice solotuion on Android...I was expecting something related to create my activity.
Ok thanks,
I will give a try to that. -
wrote on 6 Sept 2014, 09:56 last edited by
Hello thanks,
I am trying to create the code, but I don't understand what is
data->onSelectPhoto parameter in QtAndroid::startActivity() function.Could you tell me a bit more about that?
Thanks
Gianni -
Hello thanks,
I am trying to create the code, but I don't understand what is
data->onSelectPhoto parameter in QtAndroid::startActivity() function.Could you tell me a bit more about that?
Thanks
Gianniwrote on 30 Apr 2015, 22:47 last edited byhow to get the result from handleActivityResult virtual method ,when i select any action using android jni.
-
For iOS you need to use UIImagePickerController, and because it's very easy to mix Qt C++ code with Objective-C code, just look with google on a tutorial for using UIImagePickerController and you can directly use that code.
For Android the road it's a bit more difficult, this is my solution:
-
Create a custom android java Activity subclassing QtActivity, into the java class define some static method for constructing the necessary Intent:
@
public static Intent createChoosePhotoIntent() {
Intent intent = new Intent( Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI );
intent.setType("image/*");
return Intent.createChooser(intent, "Select Image");
}
@ -
Create a C++ subclass of QAndroidActivityResultReceiver for getting the result of the Intent launched (in this case the images chosen):
@
class OnSelectPhotoResult : public QAndroidActivityResultReceiver {
void handleActivityResult(int requestCode, int resultCode, const QAndroidJniObject & data) {
qDebug() << "TOTAPE RETURNING FROM SELECT PHOTO - " << resultCode << " - " << requestCode;
if ( resultCode == -1 ) {
// OK !
QAndroidJniObject imageUri = data.callObjectMethod(
"getData",
"()Landroid/net/Uri;");
QAndroidJniObject imageFile = QAndroidJniObject::callStaticObjectMethod(
"com/totape/app/TotapeActivity",
"getPath",
"(Landroid/net/Uri;)Ljava/lang/String;",
imageUri.object<jobject>());
Backend::instance()->photoReady( imageFile.toString() );
}
}
};
@
In my code above, I use java methods to process the result, this because the returned data are java object, so you need to call java methods via JNI in order to extract information. For doing so, personally I prefer to create static method in the custom Activity in order to minimize the number of JNI calls -
Launch the Intent from C++ with this code:
@
void Backend::selectPhoto() {
QAndroidJniObject intent = QAndroidJniObject::callStaticObjectMethod(
"com/totape/app/TotapeActivity",
"createChoosePhotoIntent",
"()Landroid/content/Intent;" );
qDebug() << "STARTING INTENT FOR SELECTING A PHOTO";
QtAndroid::startActivity( intent, 12051978, data->onSelectPhoto );
}
@
That's it.
This is my solution.wrote on 18 Jun 2015, 20:24 last edited by@Gianluca Can you explain the code for the android work around a little more. I am confused about what Backend does as well as what files the code should be in. Such as making a c++ subclass as well as making a subclass of QtActivity.
thank you
-
-
wrote on 22 Jun 2015, 18:59 last edited by
In the QtAndroid::startActivity() function data->onSelectPhoto what is data? You don't have it defined anywhere. I have tried QAndroidJniObject and QAndroidActivityresultHandler but it says onSelectPhoto is not a member of these types. Any help would be great!!!
-
wrote on 9 Dec 2015, 09:58 last edited by
Thanks for android I solved it. It is working very well.
-
wrote on 24 May 2019, 10:39 last edited by
This is an old thread, but maybe still interesting. There is an (multi-) image picker component for Android and iOS available with Felgo: https://felgo.com/updates/release-3-2-0-qt-5-12-3-subscriptions