package org.qtproject.qt5.android.bindings;import android.content.Intent;import android.net.Uri;import android.util.Log;import android.content.Context;import android.database.Cursor;import android.provider.MediaStore;public class FilePicker { public static Intent createChooseVideoIntent() { Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); intent.addCategory(Intent.CATEGORY_OPENABLE); intent.setType("video/mp4"); return intent; } public static String getFileName(Context context, Uri uri) { String fileName = ""; Cursor cursor = null; try { String[] projection = { MediaStore.Video.Media.DISPLAY_NAME }; cursor = context.getContentResolver().query(uri, projection, null, null, null); int nameIndex = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME); cursor.moveToFirst(); fileName = cursor.getString(nameIndex); } finally { if (cursor != null) { cursor.close(); } } return fileName; }}
QAndroidJniObject intent = QAndroidJniObject::callStaticObjectMethod( "org/qtproject/qt5/android/bindings/FilePicker", "createChooseVideoIntent", "()Landroid/content/Intent;");
const int REQUEST_CODE = 42; // Any unique number...QtAndroid::startActivity(intent, REQUEST_CODE, this);
void FilePicker::handleActivityResult(int receiverRequestCode, int resultCode, const QAndroidJniObject &data){ if (receiverRequestCode == REQUEST_CODE && resultCode == RESULT_OK) { QAndroidJniObject videoUri = data.callObjectMethod( "getData", "()Landroid/net/Uri;"); QAndroidJniObject videoFile = QAndroidJniObject::callStaticObjectMethod( "org/qtproject/qt5/android/bindings/FilePicker", "getFileName", "(Landroid/content/Context;Landroid/net/Uri;)Ljava/lang/String;", QtAndroid::androidActivity().object<jobject>(), videoUri.object<jobject>()); emit filePicked(videoFile.toString()); }}