Qt for android : 简单实现弹窗创建文件,并使用JNI进行读写实例

简介

Qt版本 : Qt6.6.2

手机: RedMi 7A

本例使用JNI方式打开窗体创建文件, handleActivityResult中处理创建文件之后通过InputStream/OutputStream简单读写文件

代码

项目添加

QT += core-private

头文件

代码来之参考中的连接, 稍微修改

需要注意的是 QAndroidActivityResultReceiver, 继承之后可以接收startActivity执行之后的返回结果,handleActivityResult槽中处理结果

cpp 复制代码
#ifndef HARDWAREUTILS_ANDROID_H
#define HARDWAREUTILS_ANDROID_H

#include "private/qandroidextras_p.h"
#include "qjniobject.h"
#include <QObject>
#include <QJniObject>
#include <QJniEnvironment>
#include <QFile>

class HardwareUtilsAndroid: public QObject, public QAndroidActivityResultReceiver
{
private:
    HardwareUtilsAndroid(QObject *parent = 0);

public:
    virtual ~HardwareUtilsAndroid();
    static HardwareUtilsAndroid* Instance(QObject *parent = 0);

    virtual void handleActivityResult(int receiverRequestCode, int resultCode, const QJniObject & data);
    virtual bool writeOnUSB(QString filename);

private:
    void writeFile(QJniObject &fileUri, const QString &text);
    void readFile(QJniObject &fileUri, QString &text);

private:
    QFile m_file;
};

#endif // HARDWAREUTILS_ANDROID_H

源文件

cpp 复制代码
#include "HardwareUtils_Android.h"
#include <jni.h>

#include <QDebug>

HardwareUtilsAndroid *HardwareUtilsAndroid::Instance(QObject *parent) {
    static HardwareUtilsAndroid *instance = NULL;

    if(!instance) {
        instance = new HardwareUtilsAndroid(parent);
    }

    return instance;
}

HardwareUtilsAndroid::HardwareUtilsAndroid(QObject *parent):
    QObject(parent)
{
}

HardwareUtilsAndroid::~HardwareUtilsAndroid()
{
}

bool HardwareUtilsAndroid::writeOnUSB(QString filename) 
{
    QJniObject ACTION_CREATE_DOCUMENT = QJniObject::fromString("android.intent.action.CREATE_DOCUMENT");
    QJniObject intent("android/content/Intent");
    if (ACTION_CREATE_DOCUMENT.isValid() && intent.isValid()) 
    {
        intent.callObjectMethod("setAction", "(Ljava/lang/String;)Landroid/content/Intent;", ACTION_CREATE_DOCUMENT.object<jstring>());
        intent.callObjectMethod("setType", "(Ljava/lang/String;)Landroid/content/Intent;", QJniObject::fromString("text/plain").object<jstring>());
        intent.callObjectMethod("putExtra", "(Ljava/lang/String;Ljava/lang/String;)Landroid/content/Intent;",
                                QJniObject::fromString("android.intent.extra.TITLE").object<jstring>(),
                                QJniObject::fromString(filename).object<jstring>());

        QtAndroidPrivate::startActivity(intent.object<jobject>(), 123, this);
        return true;
    } 
    else 
    {
        return false;
    }
}

void HardwareUtilsAndroid::writeFile(QJniObject &fileUri, const QString &text)
{
    // 获取文件输出流
    QJniObject activity = QtAndroidPrivate::activity();
    QJniObject contentResolver = activity.callObjectMethod("getContentResolver", 
                                                           "()Landroid/content/ContentResolver;");
    QJniObject outputStream = contentResolver.callObjectMethod("openOutputStream", 
                                                               "(Landroid/net/Uri;)Ljava/io/OutputStream;",
                                                               fileUri.object<jobject>());

    if (outputStream.isValid())
    {
        QJniObject stringToWrite = QJniObject::fromString(text);
        QJniObject bytes = stringToWrite.callObjectMethod("getBytes", "()[B");

        QJniEnvironment env;
        jbyteArray byteArray = static_cast<jbyteArray>(bytes.object<jarray>());
        jbyte* byteData = env->GetByteArrayElements(byteArray, nullptr);
        int length = env->GetArrayLength(byteArray);

        outputStream.callMethod<void>("write", "([B)V", byteArray);
        env->ReleaseByteArrayElements(byteArray, byteData, 0);

        outputStream.callMethod<void>("close", "()V");
        qDebug() << "File written successfully";
    }
    else
    {
        qDebug() << "Failed to open output stream";
    }
}

void HardwareUtilsAndroid::readFile(QJniObject &fileUri, QString &text)
{
    QJniEnvironment env;
    QJniObject activity = QtAndroidPrivate::activity();
    QJniObject contentResolver = activity.callObjectMethod("getContentResolver", 
                                                           "()Landroid/content/ContentResolver;");

    // 创建InputStream
    QJniObject inputStream = contentResolver.callObjectMethod("openInputStream", 
                                                              "(Landroid/net/Uri;)Ljava/io/InputStream;", fileUri.object<jobject>());

    // 创建缓冲区
    jbyteArray bufferArray = env->NewByteArray(1024);

    // 读取数据
    QByteArray fileData;
    jint bytesRead;
    while ((bytesRead = inputStream.callMethod<jint>("read", "([B)I", bufferArray)) != -1) 
    {
        jbyte* buffer = env->GetByteArrayElements(bufferArray, nullptr);
        fileData.append(reinterpret_cast<const char*>(buffer), bytesRead);
        env->ReleaseByteArrayElements(bufferArray, buffer, JNI_ABORT);
    }

    // 关闭流
    if (inputStream.isValid()) 
    {
        inputStream.callMethod<void>("close", "()V");
        if (env->ExceptionCheck()) 
        {
            env->ExceptionClear();
        }
    }

    text = QString::fromUtf8(fileData);
    // 删除局部引用
    env->DeleteLocalRef(bufferArray);
}

void HardwareUtilsAndroid::handleActivityResult(int receiverRequestCode, int resultCode, const QJniObject & data) 
{
    qDebug() << "Receive activity result";

    jint RESULT_OK = QJniObject::getStaticField<jint>("android/app/Activity", "RESULT_OK");
    if (receiverRequestCode == 123 && resultCode == RESULT_OK)
    {
        QJniObject uri = data.callObjectMethod("getData", "()Landroid/net/Uri;");
        if (!uri.isValid())
        {
            qDebug() << "Invalid URI";
            return;
        }

        QString text;
        writeFile(uri, QString("hello world!!!!"));
        readFile(uri, text);
        qDebug() << "read: " << text;
    }
    else
    {
        qDebug() << "Failed to create file";
    }
}

调用

可以搭配QFileDialog进行使用,打开U盘的路径创建文件

cpp 复制代码
HardwareUtilsAndroid::Instance()->writeOnUSB(dlg.selectedFiles().value(0));

参考:
https://github.com/jbflamant/android-usb-access

相关推荐
Blasit32 分钟前
Qt C++ QStatusbar 显示表示状态的图片
开发语言·c++·qt
木心操作36 分钟前
js使用qrcode与canvas生成带logo的二维码
开发语言·javascript·ecmascript
利刃大大3 小时前
【C语言】内存函数详解与模拟实现
c语言·开发语言
麻辣炖土豆儿3 小时前
Android BottomNavigationView不加icon使text垂直居中,完美解决。
android
siy23333 小时前
[c语言日寄]精英怪:三子棋(tic-tac-toe)3命慢通[附免费源码]
c语言·开发语言·笔记·学习·算法
疯狂的沙粒3 小时前
如何将一个数组转换为字符串?
开发语言·前端·javascript·柔性数组
shix .3 小时前
python范围
开发语言·python
源之缘-OFD先行者3 小时前
FreeType 介绍及 C# 示例
开发语言·c#
惊鸿一梦q3 小时前
qt5Core.dll是什么文件,qt5Core.dll错误怎么办
开发语言·qt