5

In my Qt Quick-based app whose first target platform is Android I need to enable the user to share an image. By that I mean the general share action on Android, which isn't limited to any particular social network:

enter image description here

I did find some threads about sharing on FB and Twitter from Qt, but they use Facebook/Twitter API directly, which is not what I want.

From what I found so far, it seems that there is no cross-platform way to do this and my application probably has to include some Java code. If this is correct, what is currently the proper way to write platform-specific code on Android? Like this?

Another idea is that it could be possible to invoke the share action via Javascript running in a website loaded in WebView. Since web apps have sharing, this should be possible and probably more cross-platform. Does that seem reasonable?

Andrej Repiský
  • 477
  • 1
  • 6
  • 19

2 Answers2

7

As far as I'm concerned. The best and actually the only practical way is to use Jni. It seems to be very confusing at first glance but if you have a little java experience, for sure you can do it.

For sending a text to another application like facebook we should use Intents. So We can simply do the job in a simple Java file and call it from c++ side using Jni. Here is the content of the SendIntent.java file. The class has a static member function that gives a context and starts the Intent. Then it sends the text data to the new activity.

package com.example.android.tools;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

public class SendIntent {
    public static void sendText(Activity context,String text) {
        Intent sendIntent = new Intent();
        sendIntent.setAction(Intent.ACTION_SEND);
        sendIntent.putExtra(Intent.EXTRA_TEXT, text);
        sendIntent.setType("text/plain");
        context.startActivity(Intent.createChooser(sendIntent, text));
    }
}

So In c++ side we just need to initiate an android activity and pass it to this class: Here is c++ code:

void example::shareText(QString str)
{
    QAndroidJniEnvironment _env;
    QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative", "activity", "()Landroid/app/Activity;");   //activity is valid
    if (_env->ExceptionCheck()) {
        _env->ExceptionClear();
        throw InterfaceConnFailedException();
    }
    if ( activity.isValid() )
    {
        QAndroidJniObject::callStaticMethod<void>("com/example/android/tools/SendIntent","sendText","(Landroid/app/Activity;Ljava/lang/String;)V",activity.object<jobject>(),QAndroidJniObject::fromString(str).object<jstring>());
        if (_env->ExceptionCheck()) {
            _env->ExceptionClear();
            throw InterfaceConnFailedException();
        }
    }else
        throw InterfaceConnFailedException();
}

If you are worried about cross platform issues you can use Preprocessor directives to write platform dependent codes, this is a very common solution in c++ programming.

And the last thing that I should mention is to add these lines of code to .pro file. So qt will be able to find the java resources too:

android {
    QT += androidextras
ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android-sources
}

In this case android-sources is the directory which I have placed all java sources.

a.toraby
  • 3,232
  • 5
  • 41
  • 73
4

I hope this post help you too: http://blog.lasconic.com/share-on-ios-and-android-using-qml/

Musa
  • 474
  • 4
  • 9