benlau/quickandroid

ImagePicker seems to work only in the example project!

Closed this issue · 5 comments

I've just included QuickAndroid to my project, created simple ImagePicker and just connected it to Image object. And it's doesn't work. I tried to understand how the example works with it, but even copying some code from it doesn't help. What's the problem? I work with ApplicationWindow and load qml pages with loader.source.
Please help me!
My project will likely to be ported on IOS devices and I should use android-specific code as little as i can.

What kind of build system are you using? ant or gradle?

Image Picker requires a Java wrapper of Android SDK. It need to setup the environment before use it.

Step 1) Call QASystemDispatcher::registerNatives() in JNI_OnLoad

Example:
https://github.com/benlau/quickandroid/blob/master/examples/quickandroidexample/main.cpp#L20

If you are using ant, then it is over at this step.

Step 2) Setup build.gradle (if you are using gradle)

Example:
quickandroid/build.gradle at DEV · benlau/quickandroid

2.1 Add this function to your build.gradle

import groovy.json.JsonSlurper

String getAndroidPackageSourceDir() {
    String res = System.getProperty("user.dir");

    FileTree tree = fileTree(dir: res + '/..').include('android*deployment-settings.json');

    if (tree.size() > 0) {
        String json = tree[0];
        def inputFile = new File(json);
        def InputJSON = new JsonSlurper().parseText(inputFile.text);
        res = InputJSON["android-package-source-directory"]
    }

    return res;
}

String androidPackageSourceDir = getAndroidPackageSourceDir();
println("ANDROID_PACKAGE_SOURCE_DIRECTORY:" + androidPackageSourceDir);

2.2 Include androidPackageSourceDir


    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = [qt5AndroidDir + '/src', 'src', 'java',
                            androidPackageSourceDir + '/../../../java']
            aidl.srcDirs = [qt5AndroidDir + '/src', 'src', 'aidl']
            res.srcDirs = [qt5AndroidDir + '/res', 'res']
            resources.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            assets.srcDirs = ['assets']
            jniLibs.srcDirs = ['libs']
       }
    }

It doesn't work for me, whith Ant it does not works too.
It says on build that

No signature of method: org.gradle.api.internal.file.collections.DefaultConfigurableFileTree.size() is applicable for argument types: () values: []
Possible solutions: is(java.lang.Object), sum(), sort(), min(), find(), with(groovy.lang.Closure)

And when i commented 'if (tree.size() > 0) {' line, it was built but ImagePicker still doesn't work.

It successfully opens the image gallery on android but evaluation of onClicked function that calls pickImage() just interrupts in the middle and nothing happens.

I use gradle 2.2.1-all and qt 5.7 on Arch Linux.

Your example without those changes in gradle.build is works and it's strange.

ah, I have missed one step.

Step 3) Setup Android Activity

If you do not have your own custom Android Activity, you may change to "quickandroid.QuickAndroidActivity"

https://github.com/benlau/quickandroid/blob/master/examples/quickandroidexample/android-sources/AndroidManifest.xml#L7

Otherwise, you need to add the following line to your custom activity:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        SystemDispatcher.onActivityResult(requestCode,resultCode,data);
    }

Reference: https://github.com/benlau/quickandroid/blob/master/java/quickandroid/QuickAndroidActivity.java#L14

It works now! Thank you.
Could you add this to the help/wiki/etc, please? It's not obvious how to use your components separately.