Does FFileHelper help in Android?

I’m trying to open an image file to create a new texture2D for an AR app. Unfortunately, I’m not sure what’s going wrong here:

	FString ResultFilePath = FString("");
	if (JNIEnv* Env = FAndroidApplication::GetJavaEnv())
	{
		jstring ResultFilePathString = (jstring)FJavaWrapper::CallObjectMethod(Env, FJavaWrapper::GameActivityThis, FMobileUtilsPlatform::GetImagePathMethod);
		const char* nativeFilePathString = Env->GetStringUTFChars(ResultFilePathString, 0);
		ResultFilePath = FString(nativeFilePathString);
		Env->ReleaseStringUTFChars(ResultFilePathString, nativeFilePathString);
		Env->DeleteLocalRef(ResultFilePathString);
		UTexture2D* LoadedTex = NULL;

		IImageWrapperModule& ImageWrapperModule = FModuleManager::LoadModuleChecked<IImageWrapperModule>(FName("ImageWrapper"));
		// Need support for other image types.
		IImageWrapperPtr ImageWrapper = ImageWrapperModule.CreateImageWrapper(EImageFormat::PNG);
		TArray<uint8> RawFileData;
		if (!FFileHelper::LoadFileToArray(RawFileData, *ResultFilePath))
		{
			return NULL;
		}

The return value from my GetImagePathMethod looks correct, I get results that look like:

/storage/emulated/0/...

And that seems correct, but, FFileHelper always returns false. The user selects the image to be used from their gallery, so, the file certainly exists but this also returns false:

FFileHelper::FileExists(*ResultFilePath)

Is there something about File IO in android that I’m getting wrong? Or am I approaching this in C++ when I should be trying to access the file in Java?
Thanks!

1 Like

Okay, so, the problem is actually fairly obvious. FFileHelper is was searching inside of GExternalFilePath/[path to image], but I needed to be at root to find the images on the device. So, in the header, outside of any class, I added:

extern FString GExternalFilePath;

Next, just a simple function to escape all the way to the root directory:

FString FMobileUtilsPlatform::GetRootPath()
{
	FString PathStr = "";
#if PLATFORM_ANDROID
	TArray<FString> Folders;
	GExternalFilePath.ParseIntoArray(Folders, TEXT("/"));
	for (FString Folder : Folders)
	{
		PathStr += FString("/..");
	}

#endif
	return PathStr;
}

And finally, update the original function:

FString ExternalFilePath = GetRootPath() + ResultFilePath;
		if (!FFileHelper::LoadFileToArray(RawFileData, *ExternalFilePath))
		{
			return NULL;
		}
1 Like