6

I'm a beginner learning LibGDX. I'm developing my first game in LibGDX where you can replace the enemy ball with selected picture (e.g. someones head) and then you dodge the enemy . In my mainmenu i have stage2d buttons and i want to have one button which lets a user choose the picture he wants to use in game. So i created a button and added a CLickListner to it. Now where do i go from here ? Is there a way to open the gallery in LibGDX and let the user choose a picture he wants to use ?

Edit :

So let's say I have an interface in my core project:

   public interface OpenGallery {
public void openGallery();
}

And in my android directory I create a Platform specific class that launches the intent

import android.content.Intent;

import com.kubas.zaidimas.OpenGallery;

public class GalleryOpener extends AndroidLauncher implements OpenGallery {

    private static final int SELECT_PICTURE = 1;

    @Override
    public void openGallery() {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent,
                "Select Picture"), SELECT_PICTURE);     
    }

What should i do from this point ? So I can call it inside my mainmenu ?

user3802649
  • 379
  • 2
  • 13
  • 1
    Take a look at this answer: http://stackoverflow.com/questions/2169649/get-pick-an-image-from-androids-built-in-gallery-app-programmatically – swbandit Jul 03 '14 at 17:21
  • Hey tried using that tutorial but it's meant for Android I think? Can i do the same in my project in Libgdx? – user3802649 Jul 05 '14 at 10:16
  • The gallery functionality is specific to each platform. From the libgdx perspective only iOS and Android have something like that and you will need to use platform-specific code because libgdx does not have anything like that (there are no galleries in html5, javascripts). This will also help you integrate platform-specific code into a libgdx project: https://github.com/libgdx/libgdx/wiki/Interfacing-with-platform-specific-code – swbandit Jul 05 '14 at 22:09
  • Thanks Mike ! You gave me atleast a basic understanding how this should work theoretically. But in practice I still can't figure out how to call android specific code from my mainmenu. I have edited my post and added some code. – user3802649 Jul 06 '14 at 15:12
  • You need to instantiate your GalleryOpener (in the starter class specific for the android platform) and call the openGallery() method when the menu/button is pressed. – swbandit Jul 07 '14 at 14:53
  • Also, I'm not sure what the `AndroidLauncher` is. I believe your class should extend an `Activity`. You should also implement the `onActivityResult()` method which will be called when the image is selected from the gallery. In that method you can store the image or do whatever you need with it. – swbandit Jul 07 '14 at 14:57
  • Still have no idea :( Just to understand can someone write an example to open gallery? When I instantiate Galleryopener in android specific class I make a new constructor for my game main class and pass in OpenGallery then in my mainmenu i create : OpenGallery gallery; and then when a button is pressed I try to call gallery.openGallery; I get a null pointer exception – user3802649 Jul 07 '14 at 22:15
  • :( And I think that by doing the above code I would just call the empty interface method ?? I'm really sorry for my noobish questions :( I'm a starter but if I could get a similar example i would probbably work out the rest myself just the only thing is that admob or any other examples are different :(Thanks! – user3802649 Jul 07 '14 at 22:16
  • I've added an answer with a sample project in github. – swbandit Jul 08 '14 at 15:26

2 Answers2

7

I've created an example libgdx project with android-specific code for invoking the library and retrieving the file path for the image selected by the user. Hope this will help you understand how the integration works. I have tested it on a couple of android devices. See https://github.com/miket-ap/libgdx-platform-specific-example

swbandit
  • 1,986
  • 1
  • 26
  • 37
  • Hey man thanks alot !!! I got it working in my main class where i call my splash screen :) But the only problem i have is how do i call it in my mainmenu ? Because my class MyGdxApp calls splashscreen and splashscreen calls mainmenu If I try MyGdxApp.galleryopener.getGallerypath in mainmenu I get a null pointer – user3802649 Jul 09 '14 at 11:26
  • It's hard for me to tell without seeing the code or the stack trace. But I'm guessing that you're calling the the MyGdxApp statically (not an instance of it) and that's why the galleryopener is null. You should pass a reference to your game in the constructor or setter. For example: MenuScreen menu = new MenuScreen(this); – swbandit Jul 09 '14 at 14:05
  • Got it to work myself! U need to add the string to your interface and then call it where you need the path; Now I try to do Texture texture = new Texture(MyGdxApp.galleryOpener.getSelectedFilePath()) but it gives me file not found exception :( Should I call another method ? – user3802649 Jul 10 '14 at 19:45
  • Great. Glad you figured it out. The problem might be that the path is not set yet (it might be blank). Log the value to logcat `Log.d(...)` to see what it is before you create the texture. – swbandit Jul 11 '14 at 02:14
  • The example project is very helpful – Dr Deo Jan 05 '17 at 10:08
1

To call the the platform-scpefic object (galleryOpener) from other classes you need to pass a reference to it using a constructor or setter:

public class MyGdxApp extends Game {
        MainMenuScreen mainMenuScreen;

       @Override
        public void create() {
                mainMenuScreen = new MainMenuScreen(this);
                setScreen(mainMenuScreen);              
        }
} 

Then in your menu screen:

public class MainMenuScreen implements Screen {

   MyGdxApp app; 


   // constructor to keep a reference to the main Game class
    public MainMenuScreen(MyGdxApp app){
            this.app = app;
    }

    @Override
    public void render(float delta) {
            // update and draw stuff
         if (Gdx.input.justTouched()) 
             app.galleryOpener.getFilePath()
    }
    ...
swbandit
  • 1,986
  • 1
  • 26
  • 37
  • Now the only thing is how do I retrieve a Imagepath in my core project ? Because now it is only in the android launcher ? – user3802649 Jul 09 '14 at 19:43
  • I updated AndroidLauncher and AndroidGalleryOpener. Take a look on github. Basically, you can store the galleryOpener in a variable and then set a property on it when you receive back the image path. After that you can get the image path from the galleryOpener. – swbandit Jul 09 '14 at 21:09
  • I did the same thing in my code :( Still cant get the string path to my core projects :( Can you tell me how exactly to get string path to MyGdxApp core project :) – user3802649 Jul 09 '14 at 22:34
  • Im trying to call galleryOpener.getSelectedFilePath() but it doesn't let me . Thanks to you I understood how to call intents but I'm still not sure how to pass in variables from android project part to core project – user3802649 Jul 10 '14 at 16:19