gpt4 book ai didi

qt - iOS 和 Android 中的照片库

转载 作者:行者123 更新时间:2023-12-04 13:56:51 27 4
gpt4 key购买 nike

我正在开发一个可在 iOS 中运行的移动应用程序和 Android我在访问 image gallery 时遇到了一些困难具有 Qml 的设备的数量.

我需要在 GridView 中列出图片库中的图像.

我尝试使用 QStandardPaths 返回图片文件夹但它只适用于台式电脑。适用于运行的智能手机 iOSAndroid它返回一个不是画廊文件夹的文件夹。

有人可以帮我弄清楚如何做到这一点吗?我的代码如下:

主程序

#include <QGuiApplication>
#include <QQmlApplicationEngine>

#include <QtQml>

#include "caminhoimagens.h"

int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);

qmlRegisterType<caminhoImagens>("PathImagens", 1, 0, "CaminhoImagens");

QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

return app.exec();
}

主文件
import QtQuick 2.4
import QtQuick.Window 2.2
import QtQuick.Controls 1.3
import QtQuick.Dialogs 1.2

import Qt.labs.folderlistmodel 2.1

import PathImagens 1.0

Window {
visible: true

width: 360
height: 640

maximumHeight: 640
minimumHeight: 640

maximumWidth: 360
minimumWidth: 360

title: "Acessar Galeria Test"

Rectangle {
id: principal

anchors.fill: parent

ListModel {
id: listModel
}

FolderListModel {
id: folderListModel

folder: "file://" + caminhoImagens.retornaCaminhoImagens()
nameFilters: "*.jpeg"
}

CaminhoImagens {
id: caminhoImagens
}

Item {
id: listaFotosDelegate

property Component delegateComponent: listaFotosDelegateComponent

Component {
id: listaFotosDelegateComponent

Image {
id: imagem

source: folderListModel.folder + "/" + fileName

width: principal.width / 4.2
height: principal.width / 4.2

fillMode: Image.PreserveAspectCrop
}
}
}

GridView {
id: listaFotosGridView

anchors.fill: parent

clip: true

model: folderListModel

delegate: listaFotosDelegate.delegateComponent

cellWidth: parent.width / 4
cellHeight: parent.width / 4
}
}
}

caminhoimagens.h
#ifndef CAMINHOIMAGENS_H
#define CAMINHOIMAGENS_H

#include <QObject>
#include <QStandardPaths>

class caminhoImagens : public QObject
{
Q_OBJECT

public slots:
QString retornaCaminhoImagens();

public:
caminhoImagens();
};

#endif // CAMINHOIMAGENS_H

caminhoimagens.cpp
#include "caminhoimagens.h"

caminhoImagens::caminhoImagens()
{

}

QString caminhoImagens::retornaCaminhoImagens()
{
return QStandardPaths::locate(QStandardPaths::PicturesLocation, QString(), QStandardPaths::LocateDirectory);
}

最佳答案

回答我自己的问题。

在 iOS 中只需创建一个 FileDialogQML file并设置 folder: shortcuts.pictures .它将调用 iOS 库。

在 Android 中,一旦需要集成 Java 代码,这是一项更难的工作。

我已经使用 QAndroidJniObject 在 Qt 中完成了我的代码编写等效的java代码。

caminhoimagens.h

#ifndef CAMINHOIMAGENS_H
#define CAMINHOIMAGENS_H

#include <QObject>

#include "imagepickerandroid.h"

#include <QDebug>

class caminhoImagens : public QObject
{
Q_OBJECT

Q_PROPERTY(QString imagemCaminho READ imagemCaminho NOTIFY imagemCaminhoChanged)

public slots:
void buscaImagem();
void retornaImagem(QString path);

public:
caminhoImagens();

QString imagemCaminho();

private:
QString m_imagemCaminho = "";

signals:
void imagemCaminhoChanged();
};

#endif //CAMINHOIMAGENS_H

caminhoimagens.cpp
#include "caminhoimagens.h"

caminhoImagens::caminhoImagens()
{

}

void caminhoImagens::buscaImagem()
{
imagePickerAndroid *imagePicker = new imagePickerAndroid();
connect(imagePicker, SIGNAL(imagemCaminhoSignal(QString)), this, SLOT(retornaImagem(QString)));

imagePicker->buscaImagem();
}

void caminhoImagens::retornaImagem(QString path)
{
qDebug() << path;

m_imagemCaminho = path;

emit imagemCaminhoChanged();
}

QString caminhoImagens::imagemCaminho()
{
return m_imagemCaminho;
}

图像选择器android.h
#ifndef IMAGEPICKERANDROID_H
#define IMAGEPICKERANDROID_H


#include <QObject>
#include <QtAndroidExtras>

#include <QDebug>

class imagePickerAndroid : public QObject, public QAndroidActivityResultReceiver
{
Q_OBJECT

public:
imagePickerAndroid();

void buscaImagem();

virtual void handleActivityResult(int receiverRequestCode, int resultCode, const QAndroidJniObject & data);

signals:
void imagemCaminhoSignal(QString);
};

#endif // IMAGEPICKERANDROID_H

图像选择器android.cpp
#include "imagepickerandroid.h"

imagePickerAndroid::imagePickerAndroid()
{

}

void imagePickerAndroid::buscaImagem()
{
QAndroidJniObject ACTION_PICK = QAndroidJniObject::fromString("android.intent.action.GET_CONTENT");
QAndroidJniObject intent("android/content/Intent");
if (ACTION_PICK.isValid() && intent.isValid())
{
intent.callObjectMethod("setAction", "(Ljava/lang/String;)Landroid/content/Intent;", ACTION_PICK.object<jstring>());
intent.callObjectMethod("setType", "(Ljava/lang/String;)Landroid/content/Intent;", QAndroidJniObject::fromString("image/*").object<jstring>());
QtAndroid::startActivity(intent.object<jobject>(), 101, this);
qDebug() << "OK";
}
else
{
qDebug() << "ERRO";
}
}

void imagePickerAndroid::handleActivityResult(int receiverRequestCode, int resultCode, const QAndroidJniObject &data)
{
qDebug() << "Trabalha com os dados";

jint RESULT_OK = QAndroidJniObject::getStaticField<jint>("android/app/Activity", "RESULT_OK");
if (receiverRequestCode == 101 && resultCode == RESULT_OK) {
QString imagemCaminho = data.callObjectMethod("getData", "()Landroid/net/Uri;").callObjectMethod("getPath", "()Ljava/lang/String;").toString();
emit imagemCaminhoSignal(imagemCaminho);

qDebug() << imagemCaminho;
}
else
{
qDebug() << "Caminho errado";
}
}

关于qt - iOS 和 Android 中的照片库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33443114/

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