how to get image from gallery in cocos2dx?

  • shahid
    Likes 0

    Problem Description

    Hello, I am using this utility to get image from gallery

    https://github.com/qiankanglai/ImagePicker

    The author of this utility says
    "Just call ImagePicker::getInstance()->pickImage() and pass in your delegate. If the image is picked successfully, your delegate will receive the corresponding Texture2D, or nullptr otherwise."

    but I dont know how to pass the delegate inside pickImage() so I am calling the function inside pickImage function directly which opens the gallery.
    So instead of ImagePicker::getInstance()->pickImage(); I am calling ImagePickerImpl::openImage();

    This opens the image gallery for me but now I need to figure out how to fetch the selected image. Please Help!!

     
     
  • Sonar Systems admin
    Likes 0

    Why aren’t you using the pickImage method?

  • shahid
    Likes 0

    @SonarSystem Thank you for your reply…. I am actually a newbie to cocos so please bear with me.. I actually dont know how to get an instance of my ImagePickerDelegate and pass it to pickImage();

    so Instead of calling  pickImage(ImagePickerDelegate *delegate)

    I did this 

    ImagePickerImpl::openImage();  

    this method successfully opens the gallery on my android device but this is not the right way to do it.. if u could tell me how to pass an instance of ImagePickerDelegate like this ImagePicker::getInstance()->pickImage(**INSTANCE OF ImagePickerDelegate**) it would solve my problem.. thanks in advance 

  • Sonar Systems admin
    Likes 0

    Take a look at the usage section on the GitHub page https://github.com/qiankanglai/ImagePicker#usage 

  • shahid
    Likes 0

    I did………. but i dont know how to pass the deleagte inside pickImage function :’(

  • Sonar Systems admin
    Likes 0

    It looks like all the code is on the github page.

  • shahid
    Likes 1

    Yeah i figured it out thanks :)

  • Sonar Systems admin
    Likes 0

    Great :D

  • shahid
    Likes 0

    Hey.. thanks for your reply.. I did manage to get the image from the gallery but the sprite in which i am loading it in is all black.

    after looking at this link

     http://discuss.cocos2d-x.org/t/android-black-sprite-loading-saved-image-from-gallery/11088/13

    I thought of using runAction(CallFunc::create([&](){}));   

    but it is not working because i think i am not using it properly this is how i am implementing it:

    void HelloWorldScene:: GoToImageGallery()
      {
      this->runAction(CallFunc::create([&](){
      Size visibleSize = Director::getInstance()->getVisibleSize();
      Director::getInstance()->setDisplayStats(false);
      Vec2 origin = Director::getInstance()->getVisibleOrigin();
      auto picker = ImagePicker::getInstance();
      picker->pickImage(_delegate);
      auto image = picker->getSelectedPhoto();
      if (image)
      {
        image->setPosition(Vec2(visibleSize.width/2+origin.x,visibleSize.height/2+origin.y));
        image->setScale((visibleSize.height / image->getContentSize().height)/2);
        this->addChild(image);
      }
      }));
      }
    

    dont understand what the problem is :(

     

     

  • Sonar Systems admin
    Likes 0

    Are you getting any logs in the console?

  • shahid
    Likes 0

    Hello.. I am using android studio.. Since I am new to cocos i dont know how to print cocos logs in android studio.. that would make life easy.. 

  • shahid
    Likes 0

    Hey I solved it ...being a noob i wasnt able to understand the author’s instructions .. after digging deep in the files i found an ImagePickerTest.cpp file which helped me get the job done! :)

  • Sonar Systems admin
    Likes 0

    Great to hear :D

     

    Feel free to post anymore questions you may have.

  • shahid
    Likes 0

    Hello again… Using this method is giving me a problem.. i do get the texture but if i use another texture then it doesnt replace the old one it create the new one over the old one.. How do i remove the old sprite data completely and add new data i.e. texture.

Login to reply