How do I share an image with users from my app written with Qt for Android
I am developing an android app using Qt. Now I'm going to share the image with users from my application. I tried to create a new intent:
package ir.qtips;
import android.content.Intent;
import android.net.Uri;
public class ShareActivity extends org.qtproject.qt5.android.bindings.QtActivity{
private static ShareActivity instance;
ShareActivity() {
instance = this;
}
public void createInstagramIntent(){
String type = "image/*";
String captionText = "<< media caption >>";
// Create the new Intent using the 'Send' action.
Intent share = new Intent(Intent.ACTION_SEND);
// Set the MIME type
share.setType(type);
// Add the URI and the caption to the Intent.
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/a.jpg"));
share.putExtra(Intent.EXTRA_TEXT, caption);
// Broadcast the Intent.
startActivity(Intent.createChooser(share, "Share to"));
}
}
And then I tried to call createInstagramIntent
from C ++:
#ifdef Q_OS_ANDROID
QAndroidJniObject jni("ir/qtips/ShareActivity");
jni.callMethod<void>("createInstagramIntent");
#endif
But that won't work. In the logs from the app, one line is more important than the others, I think:
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
source to share
You cannot create a Handler Thread from a thread that has not been called Looper.prepare( )
. What you can do is create a Handler
Thread like below using Looper
Static Class
Handler handler = new Handler(Looper.getMainLooper());
Here's an example called Android Text to Speech Service. You can follow this code to solve your problem, because Text To Speech should also be called the same asShare Intent
Show / hide example keyboard. It also needs to be called from the UI thread. This might help you.
{
public void setSuspendSleep() {
this.runOnUiThread( new Runnable() {
public void run()
{
getWindow().addFlags( WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON );
}
} );
}
public void setAllowSleep() {
this.runOnUiThread( new Runnable() {
public void run() {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
} );
}
}
------- and then in C++ ------
// getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
// getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
extern "C" void SuspendSleep( int bStopSleep )
{
static int init;
static jobject WindowManager_LayoutParams_FLAG_KEEP_SCREEN_ON;
// Attaches the current thread to the JVM.
jint lResult;
jint lFlags = 0;
JavaVM* lJavaVM = engine.app->activity->vm;
JNIEnv* lJNIEnv = engine.app->activity->env;
JavaVMAttachArgs lJavaVMAttachArgs;
lJavaVMAttachArgs.version = JNI_VERSION_1_6;
lJavaVMAttachArgs.name = "NativeThread";
lJavaVMAttachArgs.group = NULL;
lResult=lJavaVM->AttachCurrentThread(&lJNIEnv, &lJavaVMAttachArgs);
if (lResult == JNI_ERR) {
return;
}
// Retrieves NativeActivity.
jobject lNativeActivity = engine.app->activity->clazz;
jclass ClassNativeActivity = lJNIEnv->GetObjectClass(lNativeActivity);
if( bStopSleep )
{
jmethodID MethodSetFlags = lJNIEnv->GetMethodID( ClassNativeActivity, "setSuspendSleep", "()V");
if( MethodSetFlags )
lJNIEnv->CallVoidMethod( lNativeActivity, MethodSetFlags );
}
else
{
jmethodID MethodSetFlags = lJNIEnv->GetMethodID( ClassNativeActivity, "setAllowSleep", "()V");
if( MethodSetFlags )
lJNIEnv->CallVoidMethod( lNativeActivity, MethodSetFlags );
}
// Finished with the JVM.
lJavaVM->DetachCurrentThread();
}
source to share