Results 1 to 2 of 2

Thread: Android, select image using Gallery

  1. #1
    Join Date
    Mar 2015
    Posts
    1
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Windows Symbian S60 Android Maemo/MeeGo

    Default Android, select image using Gallery

    Dear all,

    I'm trying to port existing application to Android, iOS and Windows platforms. The application needs the user to select an image and on my first trial I discovered that Qt for Android uses some sort of its own dialog. Is there is a way to use the standard Android image selector (Gallery app I guess) in Qt app? If not, can I somehow write a small piece of Android code, execute it from Qt and get the filename of selected image?

    Thank you in advance

  2. #2
    Join Date
    Nov 2014
    Posts
    33
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Android, select image using Gallery

    Hello,

    The gallery doesn't return the filename of the selected image, it returns an URI. This URI is used for a content resolver to retrieve the image.

    My solution is to launch the gallery from QT code and pass the result to the Android code to retrieve the image.


    QT Code:

    Qt Code:
    1. class AndroidImagePicker: public QAndroidActivityResultReceiver
    2. {
    3. public:
    4. AndroidImagePicker() {}
    5.  
    6. void show() override {
    7. QAndroidJniObject ACTION_PICK = QAndroidJniObject::fromString("android.intent.action.GET_CONTENT");
    8. QAndroidJniObject intent("android/content/Intent");
    9. if (ACTION_PICK.isValid() && intent.isValid())
    10. {
    11. intent.callObjectMethod("setAction", "(Ljava/lang/String;)Landroid/content/Intent;", ACTION_PICK.object<jstring>());
    12. intent.callObjectMethod("setType", "(Ljava/lang/String;)Landroid/content/Intent;", QAndroidJniObject::fromString("image/*").object<jstring>());
    13. QtAndroid::startActivity(intent.object<jobject>(), 101, this);
    14. }
    15. }
    16.  
    17. virtual void handleActivityResult(int receiverRequestCode, int resultCode, const QAndroidJniObject & data) override {
    18. jint RESULT_OK = QAndroidJniObject::getStaticField<jint>("android/app/Activity", "RESULT_OK");
    19. if (receiverRequestCode == 101 && resultCode == RESULT_OK) {
    20. QAndroidJniObject activity = QtAndroid::androidActivity();
    21. QAndroidJniObject bytes = QAndroidJniObject::callStaticObjectMethod("lqsa/util/ImageFromActivityResult",
    22. "getImage",
    23. "(Landroid/content/Intent;Lorg/qtproject/qt5/android/bindings/QtActivity;)[B",
    24. data.object<jobject>(), activity.object<jobject>());
    25.  
    26. QAndroidJniEnvironment env;
    27. jbyteArray imageDataArray = bytes.object<jbyteArray>();
    28. if (imageDataArray) {
    29. jsize imageSize = env->GetArrayLength(imageDataArray);
    30. if (imageSize > 0) {
    31. jboolean isCopy;
    32. jbyte *imageBytes = env->GetByteArrayElements(imageDataArray, &isCopy);
    33. QImage image = QImage(QImage::fromData((uchar*) imageBytes, imageSize,"JPG"));
    34. env->ReleaseByteArrayElements(imageDataArray, imageBytes, JNI_ABORT);
    35. emit imageSelected(image);
    36. }
    37. }
    38. }
    39. }
    40. };
    To copy to clipboard, switch view to plain text mode 

    Android code (file ImageFromActivityResult.java):

    Qt Code:
    1. package lqsa.util;
    2.  
    3. import android.content.Intent;
    4. import org.qtproject.qt5.android.bindings.QtActivity;
    5. import java.io.InputStream;
    6. import java.io.ByteArrayOutputStream;
    7. import java.io.IOException;
    8.  
    9. public class ImageFromActivityResult {
    10.  
    11. public static byte[] getImage(Intent intent, QtActivity activity) throws IOException {
    12. InputStream is = activity.getContentResolver().openInputStream(intent.getData());
    13. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    14. int nRead;
    15. byte[] data = new byte[16384];
    16.  
    17. while ((nRead = is.read(data, 0, data.length)) != -1) {
    18. buffer.write(data, 0, nRead);
    19. }
    20. buffer.flush();
    21. return buffer.toByteArray();
    22. }
    23. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Get the default image folder of android
    By stereoMatching in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 25th November 2013, 05:26
  2. Select pixel from image
    By sergio87 in forum Qt Programming
    Replies: 4
    Last Post: 2nd May 2011, 12:48
  3. need help with photo gallery
    By rishiraj in forum Newbie
    Replies: 1
    Last Post: 9th May 2010, 06:05
  4. Image Gallery Viewer
    By sheeeng in forum Qt Programming
    Replies: 4
    Last Post: 19th December 2009, 09:55
  5. Select Region from image
    By yazwas in forum Newbie
    Replies: 4
    Last Post: 15th July 2009, 18:41

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.