Download

FFileHelper in Android

Hello!

I’m trying to open up an image using FFileHelper in Android. I dispatch an intent to allow the user to choose their gallery app, then select the image from that gallery. Then I’m able to get the filepath from an onActivityResult addition. So, I’m getting a string filepath that looks correct:


"/storage/emulated/0/..../imageName.png". 

Unfortunately, this filepath returns a false when I check FFileHelper::FileExists() or when I try to load it into an array. All of my searching has concluded that FFileHelper is intended to be multiplatform, so, I’m almost positive I’m just making some dumb mistake. I’d really like to know what I’m doing wrong before I move on to a much more complicated (and likely RAM intensive) solution. Here’s my answerhub post. And here’s some code snips:

The device I’m testing on is running Marshmallow… Perhaps I need to actually prompt the user to accept read external permissions?



	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; // Function always returns here. I've also checked if file exists before this point and that returns false as well.
		}


EDIT: I figured out a solution. It’s actually fairly obvious. Here’s what I did:

In my header, outside of any classes, I declared:



extern FString GExternalFilePath;


Then I wrote a new function to get the path that I needed to pass into FFileHelper::LoadFileToArray(…):



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

#endif
	return PathStr;
}


This function simply loops through the split array of folder paths to add the correct number of escapes. Notice preprocesser directives to protect GExternalFilePath from being accessed on other platforms.

Leaving this here for anyone else that might need it. I welcome any criticism, thanks!

hi skyler, I had the same discussion here with Mr Chris Babcock Access main.obb.png - Android Development - Unreal Engine Forums