Read file error - Android

I get error when trying to read a file from storage on android. Permission is issued. I don’t know you well Anrdoid programming, so could miss something obvious. Any ideas? I’d be very thankful for any pointing.

First, I copy my file in non-pak folder in the Content:

Then, check if the file exists. It does.

			if (FPaths::FileExists(FileNameFull))
			{
				UE_LOG(LogTemp, Log, TEXT("Whisper initialization from file: %s"), *FileNameFull);
				WhisperContext = whisper_init_from_file_with_params(TCHAR_TO_ANSI(*FileNameFull), whisper_context_default_params());

It exists. My log here is:

[2024.02.08-13.23.29:089][ 0]LogTemp: Whisper initialization from file: ../../../YnnkAndroid/Content/Whisper/ggml-tiny.bin

Plus, I see Whisper/ggml-tiny.bin in the obb package. Then, in the whisper_init_from_file_with_params(…) function:

    WHISPER_LOG_INFO("%s: loading model from '%s'\n", __func__, path_model);    
    auto fin = std::ifstream(path_model, std::ios::binary);
    if (!fin) {
        WHISPER_LOG_ERROR("%s: failed to open '%s'\n", __func__, path_model);
        return nullptr;
    }

And std::ifstream can’t initialize from the file. The log is:

[2024.02.08-13.23.29:089][  0]LogTemp: whisper_log: whisper_init_from_file_with_params_no_state: loading model from '../../../YnnkAndroid/Content/Whisper/ggml-tiny.bin'
[2024.02.08-13.23.29:089][  0]LogTemp: Error: whisper_log: whisper_init_from_file_with_params_no_state: failed to open '../../../YnnkAndroid/Content/Whisper/ggml-tiny.bin'

At first I tried to use Vosk, and it couldn’t initialize model from file as well, but I decided it happens because I incorrectly integrated third-party library. Whisper doesn’t use external libraries, but the problem is still here.

Android

There are a lot of threads about it, but with no solution. Here is mine (based on PathToAndroidPaths function in AndroidPlatfromFile.cpp)

#if PLATFORM_ANDROID
#include "GenericPlatform/GenericPlatformFile.h"
#include "HAL/PlatformProcess.h"
#include "Android/AndroidPlatformFile.h"
#include "Misc/App.h"
#endif

namespace PlatformFileHelpers
{
	void NormalizePath(FString& Path)
	{
		Path.ReplaceInline(TEXT("\\"), TEXT("/"), ESearchCase::CaseSensitive);
		Path.ReplaceInline(TEXT("//"), TEXT("/"), ESearchCase::CaseSensitive);
		Path.ReplaceInline(TEXT("/./"), TEXT("/"), ESearchCase::CaseSensitive);
	}

	FString GetPlatformPath(FString Path)
	{
#if PLATFORM_ANDROID
		auto& PlatformFile = IAndroidPlatformFile::GetPlatformPhysical();
		NormalizePath(Path);

		while (Path.StartsWith(TEXT("../"), ESearchCase::CaseSensitive))
		{
			Path.RightChopInline(3, false);
		}
		Path.ReplaceInline(FPlatformProcess::BaseDir(), TEXT(""));
		if (Path.Equals(TEXT(".."), ESearchCase::CaseSensitive))
		{
			Path = TEXT("");
		}
		// Local filepaths are directly in the deployment directory.
		// FileBasePath = GFilePathBase/UnrealGame/FApp::GetProjectName()/
		FString BasePath = PlatformFile.ConvertToAbsolutePathForExternalAppForRead(TEXT("../"));
		Path = BasePath / Path;

		NormalizePath(Path);
		return Path;
#else
		return FPaths::ConvertRelativePathToFull(Path);
#endif
	}
}

Usage:

FString PlatformModelDirectory = PlatformFileHelpers::GetPlatformPath(ModelDirectory);

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.