Open the Android Photo Gallery and get photo

Hi guys

How can i get a photo from Android Device.

i want to open Android Gallery of photos and pick one and send back to my game.

i saw that post How to access iOS photo library / use Objective-C properly? - Plugins - Unreal Engine Forums

but i’m a newbie on c++ until now i dont know how to convert that code to work on Android

plz help

Hi ArkanSephin,

This involves adding code on the Java side to trigger the intent like this:


	public void AndroidThunkJava_OpenGallery()
	{
             Intent Intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
             startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
	}


You would add this to GameActivity.java and add C++ calling in AndroidJNI.h and AndroidJNI.cpp to this method. To get the picked image you’d need to check in onActivityResult() for requestCode == RESULT_LOAD_IMG, resultCode == RESULT_OK, and data != null. The data then would need to be resolved to get access to the image and fed back to C++ depending on what you wanted to do with it.

Hm… i’ll try
i’m newbie on c++ but i think i can do
thx for replay

Hey I am trying to do the same thing and I was wondering if anyone has successfully implemented this since this was first posted? As a fellow newbie to c++ and Android development, I found this article helpful for understanding Android Intents: Android Intents - Tutorial

From what I understand, developing for Android requires Java. When developing through Unreal, do we ever need to touch Java, or does the c++ compile into/call the necessary Java code?

Wish this was a built in blueprint function of the engine as would love to do something similar as well…

or just basic file rename delete in the internal project directory. We can currently open and save such as .png files saved after photo function. But you could never delete them or rename from a blueprint project(on mobile) Rama makes a great plugin for windows.

did you manage to do it
i really need to know how

did you find any solution ya bro ?

actually, yes but I didn’t get it right yet
I asked ChatGPT about it gave me an error about defining something but at least it’s something
and here is his answer
To allow a player to upload an image from their gallery in an Unreal Engine mobile game, you will need to work with the platform’s file handling capabilities and integrate some mobile-specific features into Unreal Engine. Here’s a step-by-step guide on how to implement this:

Step 1: Setup Project for Mobile Development

Ensure your Unreal Engine project is set up for mobile development:

  1. Enable Mobile Platforms: In your project settings, ensure that iOS and/or Android platforms are enabled.
  2. Set Up Packaging: Set up the packaging for your mobile game under “Project Settings” → “Packaging.”

Step 2: Create a C++ Class to Handle File Upload

You’ll need to create a custom C++ class that can handle file operations, specifically for accessing the gallery and uploading images.

  1. Create a New C++ Class:
  • Go to “File” → “New C++ Class.”
  • Choose Actor as the parent class and name it something like ImageUploader.
  1. Include Necessary Headers: You’ll need to include platform-specific headers to access file operations.

cpp

Copy code

#include "ImageUploader.h"
#include "Runtime/Core/Public/Misc/FileHelper.h"
#include "Runtime/Core/Public/Misc/Paths.h"
#include "Runtime/Core/Public/GenericPlatform/GenericPlatformFile.h"
  1. Create a Function to Access the Gallery: You’ll need platform-specific code to access the gallery. Since Unreal doesn’t natively support gallery access, you’ll use platform-specific APIs. Here’s an example for Android:

cpp

Copy code

#if PLATFORM_ANDROID
#include "Android/AndroidJNI.h"
#include "Android/AndroidApplication.h"
#endif

Inside your ImageUploader.cpp, you can write a method to access the gallery and load the image:

cpp

Copy code

FString UImageUploader::PickImageFromGallery()
{
#if PLATFORM_ANDROID
    if (JNIEnv* Env = FAndroidApplication::GetJavaEnv())
    {
        jclass Class = FAndroidApplication::FindJavaClass("com/yourcompany/yourgame/YourActivity");
        jmethodID Method = Env->GetStaticMethodID(Class, "OpenGallery", "()Ljava/lang/String;");
        jstring Result = (jstring)Env->CallStaticObjectMethod(Class, Method);

        const char* NativeString = Env->GetStringUTFChars(Result, 0);
        FString ImagePath = FString(UTF8_TO_TCHAR(NativeString));
        Env->ReleaseStringUTFChars(Result, NativeString);

        return ImagePath;
    }
#endif
    return FString();
}

This code assumes you have an Android-specific method called OpenGallery in your Java code that opens the gallery and returns the path of the selected image. You would need to implement this part in your Android project.
4. Load and Display the Image: Once you have the image path, you can use Unreal’s file loading functions to load the image into your game:

cpp

Copy code

bool UImageUploader::LoadImage(FString ImagePath, UTexture2D*& OutTexture)
{
    TArray<uint8> FileData;
    if (FFileHelper::LoadFileToArray(FileData, *ImagePath))
    {
        // Code to convert FileData to UTexture2D
        return true;
    }
    return false;
}

Step 3: Integrate with Blueprints

  1. Create Blueprint Callable Functions:
  • In your ImageUploader.h, mark the functions as BlueprintCallable so you can call them from Blueprints.

cpp

Copy code

UFUNCTION(BlueprintCallable, Category = "ImageUpload")
FString PickImageFromGallery();

UFUNCTION(BlueprintCallable, Category = "ImageUpload")
bool LoadImage(FString ImagePath, UTexture2D*& OutTexture);
  1. Expose Functions to Blueprints: After compiling, these functions will be available in your Blueprints, and you can call them when you want to allow the player to upload an image.

Step 4: Implement Java Code for Android (Optional)

If you’re targeting Android, you need to implement the OpenGallery method in your Android project.

  1. Edit Your GameActivity.java:
  • Add the method to handle opening the gallery.

java

Copy code

public static String OpenGallery() {
    Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(intent, REQUEST_CODE);
    // Handle result in onActivityResult and return the file path
}
  1. Handle Result and Return Image Path: Handle the result of the image selection and return the image path to your Unreal Engine C++ code.

Step 5: Testing

  • Build and Deploy: Build the project and deploy it to your mobile device.
  • Test Functionality: Make sure the image upload functionality works as expected, and the image displays correctly in your game.

This process involves both Unreal Engine’s C++ code and some platform-specific code, especially for Android. For iOS, a similar process would involve using Objective-C or Swift to access the gallery.

Let me know if you need further assistance or details on any specific part of the implementation!