gpt4 book ai didi

android - 如何让用户分享我使用 Qt for Android 编写的应用程序中的图像

转载 作者:太空狗 更新时间:2023-10-29 12:40:49 26 4
gpt4 key购买 nike

我正在使用 Qt 开发 Android 应用程序。现在我要让用户分享我的应用程序中的图像。我试图创建新的 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"));
}

}

然后我尝试从 C++ 调用 createInstagramIntent:

#ifdef Q_OS_ANDROID
QAndroidJniObject jni("ir/qtips/ShareActivity");
jni.callMethod<void>("createInstagramIntent");
#endif

但它不起作用。在来自应用程序的日志中,我认为一行比其他行更重要:

 java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

最佳答案

您不能创建 Handler Thread来自尚未调用 Looper.prepare( ) 的线程。您可以做的是创建一个 Handler 线程,如下所示使用 Looper 静态类

Handler handler = new Handler(Looper.getMainLooper());

这是一个 example project调用了 Android Text to Speech Service。您可以按照此代码来解决您的问题,因为 Text To Speech 的调用方式也应与 Share Intent

相同

显示/隐藏键盘示例,它也需要从 UI 线程调用。它可能会帮助您。

{
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();
}

关于android - 如何让用户分享我使用 Qt for Android 编写的应用程序中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26414386/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com