Qt 和 Android Gallery - 使用 QAndroidJniObject 的 MediaStore
Posted
技术标签:
【中文标题】Qt 和 Android Gallery - 使用 QAndroidJniObject 的 MediaStore【英文标题】:Qt and Android Gallery - MediaStore using QAndroidJniObject 【发布时间】:2015-12-08 18:35:50 【问题描述】:我正在编写一个需要从 android Gallery 获取图像路径的代码。该代码是使用QAndroidJniObject
编写的,以在Qt
中编写等效的java
代码
我的问题是我没有弄清楚如何让Android MediaStore columns
返回图像路径。我尝试了几种方法都没有成功:
QAndroidJniObject dadosAndroid = QAndroidJniObject::getStaticObjectField("android/provider/MediaStore$MediaColumns", "DATA", "Ljava/lang/String;")
QAndroidJniObject dadosAndroid = QAndroidJniObject::fromString("MediaColumns.DATA")
QAndroidJniObject dadosAndroid = QAndroidJniObject::getStaticObjectField("android/provider/MediaStore", "DATA", "Ljava/lang/String;")
QAndroidJniObject dadosAndroid = QAndroidJniObject::getStaticObjectField("android/provider/MediaStore.Images.Media", "EXTERNAL_CONTENT_URI", "Landroid/net/Uri;")
QAndroidJniObject dadosAndroid = QAndroidJniObject::getStaticObjectField("android/provider/MediaStore.Images.ImageColumns", "BUCKET_DISPLAY_NAME", "Landroid/net/Uri;")
我该怎么做?我的完整代码如下。
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)
QAndroidJniObject uri = data.callObjectMethod("getData", "()Landroid/net/Uri;");
QAndroidJniObject dadosAndroid = QAndroidJniObject::getStaticObjectField("android/provider/MediaStore", "DATA", "Ljava/lang/String;");
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>(), 0, 0, 0, 0);
cursor.callMethod<jboolean>("moveToFirst", "()Z");
jint columnIndex = cursor.callMethod<jint>("getColumnIndexOrThrow", "(Ljava/lang/String;)I", dadosAndroid.object<jstring>());
//columnIndex returns 0
QAndroidJniObject result = cursor.callObjectMethod("getString", "(I)Ljava/lang/String;", columnIndex);
else
qDebug() << "Caminho errado";
【问题讨论】:
【参考方案1】:回答我自己的问题。
这是handleActivityResult
的代码,用于使用用Qt 编写的java 等效代码从android gallery
获取完整的image path
:
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";
【讨论】:
以上是关于Qt 和 Android Gallery - 使用 QAndroidJniObject 的 MediaStore的主要内容,如果未能解决你的问题,请参考以下文章
Android攻城狮Gallery和ImageSwitcher制作图片浏览器