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.
↧