提问人:Kurt Dewulf 提问时间:5/28/2023 最后编辑:Kurt Dewulf 更新时间:5/29/2023 访问量:88
QT6 Android:与其他应用程序共享文件
QT6 Android : sharing a file with other apps
问:
在 QT6 Android 应用程序中,我尝试共享文本文件,例如与 google drive 或 whatsapp 或邮件共享。 此时,我的代码打开了可能要共享的应用程序,但要附加的文件未发送,消息始终为空。
请参阅下面的代码,我认为与jniDocumentPath有关。 为了测试文件是否可读,我通过读取文件来测试它。
void myapp::shareTextDocument(const QString& documentPath)
{
qDebug() << documentPath;
QString m_downloadfilename;
m_downloadfilename = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
//test if file is readable -> result is positive
QFile inputFile(documentPath);
if (inputFile.open(QIODevice::ReadOnly))
{
QTextStream in(&inputFile);
while (!in.atEnd())
{
QString line = in.readLine();
qDebug() << line;
}
inputFile.close();
}
//text is readable.
QJniObject jniDocumentPath = QJniObject::fromString(documentPath);
// Create an Intent to share the text document
QJniObject intent("android/content/Intent",
"(Ljava/lang/String;)V",
jniDocumentPath.object<jstring>());
// Set the action and MIME type
QJniObject action = QJniObject::getStaticObjectField<jstring>(
"android/content/Intent", "ACTION_SEND");
QJniObject mimeType = QJniObject::fromString("text/plain");
intent.callObjectMethod(
"setAction", "(Ljava/lang/String;)Landroid/content/Intent;", action.object());
intent.callObjectMethod(
"setType", "(Ljava/lang/String;)Landroid/content/Intent;", mimeType.object());
// Get the current QtActivity and start the sharing activity
QNativeInterface::QAndroidApplication::runOnAndroidMainThread
([intent]() {
QJniObject currentActivity = QtAndroidPrivate::activity();
QJniObject chooserIntent = QJniObject::callStaticObjectMethod(
"android/content/Intent", "createChooser",
"(Landroid/content/Intent;Ljava/lang/CharSequence;)Landroid/content/Intent;",
intent.object(), QJniObject::fromString("Share via").object());
currentActivity.callMethod<void>(
"startActivity", "(Landroid/content/Intent;)V", chooserIntent.object());
});
}
与whatsapp邮件或谷歌驱动器等其他应用程序共享文件... 所有共享文件的应用程序都弹出,选择正常,发送正常,但文件未附加
响应后。我已经用文件提供程序更改了代码。但 仍然有同样的问题。接收器应用程序(驱动器、电子邮件、whatsapp、 ...)在未找到数据的情况下进行响应。新代码下方
//Add in the AndroidManifest file
<application .......
<provider android:name="androidx.core.content.FileProvider" android:authorities="org.my.myap.authority" android:exported="false" android:grantUriPermissions="true">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/filepaths"/>
</provider>
</application>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
//
//Add in the c++ code
void myApp::shareTextDocument(const QString& documentPath)
{
QJniObject activity = QtAndroidPrivate::activity();
QJniObject context = QtAndroidPrivate::context();
QJniEnvironment env;
QJniObject file = QJniObject::fromString(QDir::toNativeSeparators(documentPath));
QString providerAuthority = "org.my.myap.authority";
QJniObject provider = QJniObject::callStaticObjectMethod("androidx/core/content/FileProvider",
"getUriForFile",
"(Landroid/content/Context;Ljava/lang/String;Ljava/io/File;)Landroid/net/Uri;",
context.object(),
QJniObject::fromString(providerAuthority).object(),
file.object<jobject>());
QJniObject intent("android/content/Intent");
intent.callObjectMethod("setAction", "(Ljava/lang/String;)Landroid/content/Intent;", QJniObject::fromString("android.intent.action.SEND").object());
intent.callObjectMethod("putExtra", "(Ljava/lang/String;Landroid/os/Parcelable;)Landroid/content/Intent;", QJniObject::fromString("android.intent.extra.STREAM").object(), provider.object());
intent.callObjectMethod("setType", "(Ljava/lang/String;)Landroid/content/Intent;", QJniObject::fromString("application/octet-stream").object());
QtAndroidPrivate::startActivity(intent.object(), 0);
}
//Add in the CMakefilelists.txt
if(ANDROID)
set(ANDROID_XML_FILES android/res/xml/filepaths.xml)
qt_add_resources(TARGET HyundaiMeteoQML SOURCES ${ANDROID_XML_FILES})
endif()
//
//The filspaths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>
//
答: 暂无答案
评论
//text is readable
不。也许不是。如果不是,你就不会停下来。你盲目地继续。