Qt 和 Android - 从图库中的图像获取路径

Posted

技术标签:

【中文标题】Qt 和 Android - 从图库中的图像获取路径【英文标题】:Qt and Android - Get path from image in gallery 【发布时间】:2015-11-30 19:40:33 【问题描述】:

我已经编写了一个代码来从android gallery 获取图像。我正在使用QtAndroid 在 Qt 中编写等效的 java 代码。

我已经打开了图库,选择了图片并收到了路径,但是它返回了文件的MediaColumn,我需要绝对路径。

如何使用QAndroid获取文件的绝对路径在Qt中编写Java等效代码?

这是我做的代码:

imagepickerandroid.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

imagepickerandroid.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";
    

【问题讨论】:

【参考方案1】:

回答我自己的问题。

这里是使用javaQt 编写的等效代码从android gallery 获取full image path 的代码:

#include "imagepickerandroid.h"

imagePickerAndroid::imagePickerAndroid()




void imagePickerAndroid::buscaImagem()

    QAndroidJniObject ACTION_PICK = QAndroidJniObject::getStaticObjectField("android/content/Intent", "ACTION_PICK", "Ljava/lang/String;");
    QAndroidJniObject EXTERNAL_CONTENT_URI = QAndroidJniObject::getStaticObjectField("android/provider/MediaStore$Images$Media", "EXTERNAL_CONTENT_URI", "Landroid/net/Uri;");

    QAndroidJniObject intent=QAndroidJniObject("android/content/Intent", "(Ljava/lang/String;Landroid/net/Uri;)V", ACTION_PICK.object<jstring>(), EXTERNAL_CONTENT_URI.object<jobject>());

    if (ACTION_PICK.isValid() && intent.isValid())
    
        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)

    jint RESULT_OK = QAndroidJniObject::getStaticField<jint>("android/app/Activity", "RESULT_OK");
    if (receiverRequestCode == 101 && resultCode == RESULT_OK)
    
        QAndroidJniObject uri = data.callObjectMethod("getData", "()Landroid/net/Uri;");
        QAndroidJniObject dadosAndroid = QAndroidJniObject::getStaticObjectField("android/provider/MediaStore$MediaColumns", "DATA", "Ljava/lang/String;");
        QAndroidJniEnvironment env;
        jobjectArray projecao = (jobjectArray)env->NewObjectArray(1, env->FindClass("java/lang/String"), NULL);
        jobject projacaoDadosAndroid = env->NewStringUTF(dadosAndroid.toString().toStdString().c_str());
        env->SetObjectArrayElement(projecao, 0, projacaoDadosAndroid);
        QAndroidJniObject contentResolver = QtAndroid::androidActivity().callObjectMethod("getContentResolver", "()Landroid/content/ContentResolver;");
        QAndroidJniObject cursor = contentResolver.callObjectMethod("query", "(Landroid/net/Uri;[Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;)Landroid/database/Cursor;", uri.object<jobject>(), projecao, NULL, NULL, NULL);
        jint columnIndex = cursor.callMethod<jint>("getColumnIndex", "(Ljava/lang/String;)I", dadosAndroid.object<jstring>());
        cursor.callMethod<jboolean>("moveToFirst", "()Z");
        QAndroidJniObject resultado = cursor.callObjectMethod("getString", "(I)Ljava/lang/String;", columnIndex);
        QString imagemCaminho = "file://" + resultado.toString();
        emit imagemCaminhoSignal(imagemCaminho);
    
    else
    
        qDebug() << "Caminho errado";
    

【讨论】:

我在很多手机上测试过,通常没问题。但是今天我发现在装有 Android 5 的 Xperia Z1 上它总是返回一个空字符串。你注意到这个问题了吗? 不,我没有。我只在其他型号上测试过。 这似乎是 Android 5.1.1 的错误,请参阅此处***.com/questions/30567217/… 使用该问题中提供的建议修复此代码会很棒。 如我所见,您需要创建一个方法来返回uri,该方法在您上面发布的链接中称为`handleImageUri`,并将此uri用作handleActivityResult中的参数 handleActivityResult 并没有被我调用(我第一次尝试从 C++ 调用 java)虽然画廊应用程序已打开。

以上是关于Qt 和 Android - 从图库中的图像获取路径的主要内容,如果未能解决你的问题,请参考以下文章

[Android - Kitkat ]以编程方式从 Android 的内置图库应用程序中获取/选择图像

Android:从图库中获取图像,在回收器适配器中显示它们

以编程方式从 Android 的内置图库应用程序中获取/选择图像

Android从图库中获取图像会旋转

Android从图库中获取图像

Xamarin Android - 从图库中选择图像并获取其路径