How to get the absolute path of a FileHandle in libgdx - libgdx

FileHandle seems to only provide functions to get relative paths to a actual file.
How to get the absolute path of a FileHandle?

FileHandle provides a function called file() it returns a File object which may be used to call getAbsolutePath()
in short: String absolutePath = fileHandle.file().getAbsolutePath();

Related

How to tell PhpStorm that function argument is file path

In PhpStorm (and other JetBrains IDE), is it possible to make function attributes as file / resource path?
E.g. in this function:
function mix($file): string
{
// check mix maninfest for $file and return path from mix manifest
return $fire_path_with_cachebuster;
}
I'd like PHP to suggest files in the project as I define $file attribute when calling mix function.
Only manually for the moment, when calling that function 😒. And it's a temp injection (for a session) so it's not convenient:
mix('')
Place caret inside the string parameter
Use Alt + Enter (or via light bulb icon) to bring the Quick Fix / Intentions menu (on some Keymaps it might be different one)
Use "Inject language or reference" option
Then choose "File Reference" entry there (just start typing to filter the list).
The result:
Hopefully they will implement the following tickets for a permanent solution:
Using #[Language] PHP attribute at the function declaration: https://youtrack.jetbrains.com/issue/WI-56996
Or in-place via PHPDoc-like comment (before the parameter when calling that function): https://youtrack.jetbrains.com/issue/WI-20028
Watch those tickets (star/vote/comment) to get notified on any progress (and hopefully speed it up by bringing dev's attention).
Like LazyOne stated, there is currently no way to declare a parameter as being a file reference.
However, you can get a more permanent File Reference "injection" by [mis]using __DIR__.
PhpStorm considers a string mixed with the __DIR__ constant to be a file path:
It isn't perfect as it depends on what directory you are currently located in. If you only want the filename passed to your method, you can wrap the string in basename, or handle that from within your method.
echo mix(basename(__DIR__ . '/slack_bot.php'));

Using a relative path for File() in Flutter

I am working in Flutter and trying to open a json file in my assets folder. I found I can use the File() method, but it only seems to take an absolute path. Is there a way I can convert this to a relative path? I've tried using the relative path to the file already, but it returns an error saying no such file.
Here is the code so far. Basically I want to get the json file, and return it as a string (in the function readFileSync() below). Then I use that data to create a List object. If there's a better way to read a file into Flutter, I'm open to that too!
List<Answers> myFunction2() {
String arrayObjsText = readFileSync();
//print(arrayObjsText);
var tagObjsJson = jsonDecode(arrayObjsText)['tags'] as List;
var tagObjs =
tagObjsJson.map((tagJson) => Answers.fromJson(tagJson)).toList();
return tagObjs;
}
String readFileSync() {
String contents = new File(
'/Users/pstumbaugh/Documents/Computer Science/CS492 Mobile Dev/Dart-Flutter-CallMeMaybe/project3/assets/answers.json')
.readAsStringSync();
return contents;
}
I don't know much about how Futures work. I tried with those, but it seems like it always returns a Future and I'm not sure how to unpack that down to just a string without having to make everything async functions, which then led to problems when I try to get the List in my widgets on the main page...
You should to get assets not from relative path from your PC. When you install an app for a device or a emulator/simulator, it is can't access files on your computer. In few words, you can do it with loadString method from flutter/services.dart package (it is in Flutter SDK by default):
import 'package:flutter/services.dart' show rootBundle;
final data = rootBundle.loadString('assets/answers.json');
And make sure that you declared assets in pubspec.yaml config. Here is an official tutorial for how to work with assets.

GetFileFromPathAsync giving "The parameter is incorrect" exception

I've started to get into Windows Universal Platform programming (Windows 10, VS2015, c#) and hitting some basic issues..
I have a subfolder in MusicLibrary with a file test.txt in it. At the moment I am forcing GetFileFromPathAsync to be non asych (I'm in early stage of converting an old app to new target so asynch will come in later). Based on some examples the following code should work
string parentPath = returnStorage(_location).Path;
string filePath = Path.Combine(parentPath, _filename);
StorageFile _file = StorageFile.GetFileFromPathAsync(filePath).AsTask().ConfigureAwait(false).GetAwaiter().GetResult();
But I'm getting the exception "The parameter is incorrect" but I cannot understand why
_filename = "ggmptest_04a\\test.txt"
parentPath = ""
_location is musicLibrary
Elsewhere in my code I am able to create subfolders and files to musicLibrary and list files there with GetItemsAsync.. But this GetFileFromPathAsync call fails me..
Please, what is the obvious that everybody else sees immediately -- but evades now my eyes.?
GetFileFromPathAsync throws ArgumentException if the provided path is not an absolute path.
In your case, seems like that the filePath contains a relative path since parentPath is an empty string and combining it with _filename will result a relative path.

file inside a package

I want to use the as3 File() method to import an xml into a file.
The file is inside my project in the package resources/xml/baseXml.xml
Now the File() method has several properties like:
applicationDirectory, applicationStorageDirectory, desktopDirectory, documentsDirectory
But none of them points me in the right direction. So how should i do this? To get the xml file inside the package?
I have tried to embed the file to be then i have a Class and not a File.
Do you NEED to use File()? What if you were to just get the xml file with httpService?
Try:
<s:HTTPService url="resources/xml/baseXml.xml" result="yourResultHandlerToParseTheXML(event)"/>
Or you could just do:
<fx:XML source="resources/xml/baseXml.xml"/>
And parse off of that.
(Both would be located inside your tags.
Unless you need an ActionScript only solution in which case the below SHOULD work:
var myXML:XML = new XML();
myXML.ignoreWhite = true;
myXML.load("resources/xml/baseXml.xml");

Packaging External File Dependencies in Flash

I just started learning ActionScript, so maybe this is obvious. I'm loading in .txt files to generate content for my Flash application. However, I can't seem to find a way to package the .txt files with the .swf when I publish my application. I'd like to be able to run the .swf from anywhere without it depending on the files. Is there a solution for this?
Thanks!
this is an excellent post by Emanuele Feronato:
how-to-embed-a-text-file-in-flash
Basically you use the embed syntax to embed the file as a ByteArray. The key method here is to call the toString() method on the ByteArray to convert to a string.
Hope this helps
Try loading .as files, with variables defined inside them. They're, technically, still text files and still easy to edit, but they get automatically encapsulated in the .swf.
In your main stage, you just add include 'included.as';. In your included.as file, you just define whatever variables you want, for example:
var someData:Array = new Array();
someData.push('This is some string');
someData.push('This is some other string');
After you include the as, you can call whatever variable you defined. And when you export, the .as file is embedded into the .swf.
Even better then just embedding a .txt with your flash, if you're more versed with programming I would suggest you to create a static class to store the values of your application.
Just like:
class StaticData {
static public var siteTitle:String = "My site name";
static public var homeMessage:String = "Lorem Ipsum dolor sit amet...";
static public var welcomeMessage:String = "Hello World";
static public var siteWidth:Number = 1024;
static public var siteHeight:Number = 780;
}
This is a safe and realiable way to store static values using a simple class structure, to use a value from the class, you just need to call inside your flash:
my_textfield.text = StaticData.siteTitle;
my_shape.width = StaticData.siteWidth;