Android replaces certain characters in the filename with ASCII hex value - android

I'm creating files in my app with '{' and '}' in the filename (e.g. {foo}.xml). However, the special characters are being replaced with the ASCII hex values instead (i.e. {foo}.xml is created as %7Bfoo%7D.xml). Any thoughts on how to get around this and have it actually create '{foo}.xml' ?

new File("/sdcard/{file name}.xml").createNewFile() successfully creates a file with name "{file name}.xml" on SD card in my case.
new File(context.getFilesDir() + "/{file name}.xml").createNewFile() successfully creates such file in private app files area.
I checked that all right using ADB shell and file explorer.

Related

Converting .m3u playlists from Windows for Android media players using Notepad++

Winamp saves playlists that are saved in same folder as the music as relative paths for Windows, but copying and pasting into Android doesn't work unless I convert it to Linux relative paths. So
#EXTM3U
#EXTINF:262,Corona - Rhythm Of The Night
Unsorted\Corona - Rhythm Of The Night.mp3
#EXTINF:324,The B-52's - Love Shack
The B-52's - Love Shack.mp3
needs conversion to
#EXTM3U
#EXTINF:262,Corona - Rhythm Of The Night
./Unsorted/Corona - Rhythm Of The Night.mp3
#EXTINF:324,The B-52's - Love Shack
./The B-52's - Love Shack.mp3
for VLC Player on Android to read the playlist properly.
Well, figuring out how to convert \ to / on Notepad++ without regular expressions enabled was easy enough, but I'm too new at regex to get a grip on how to even read the table of contents on its guides even though all I want to do after that is to add ./ to the start of every odd line after the first line.
You may use
(?:.*\R){2}\K
and replace with ./.
Details
(?:.*\R){2} - two consecutive occurrences ({2}) of any 0+ chars other than line break chars, as many as possible (.*),
\K - match reset operator discarding all text matched so far from the match buffer.
The replacement is ./, i.e. it is inserted at the end of the match.
The method I use is, open playlist in VLC for windows; then in VLC:
[Menu] Media
[select] {Save Playlist to File}
[edit] {File name}
[select] {Save as type} M3U playlist (*.m3u)
[Enter]
Then I open in Notepad++, and do the following two search & replaces:
1) Change line termination characters
Find -- "CRLF"
\r\n
Replace with {Extended} -- "LF"
\n
[Select] {Search Mode} Extended
[Select] {Replace All}
2) Change/replace path separation characters
Find -- ASCII Encoded "\"'s -- value:
%5C
Replace with -- ASCII Encoded "/"'s -- value:
%2F
[Select] {Search Mode} Normal
[Select] {Replace All}
You do not need to use regular expressions for this.
Just replace "/" with "\", it's that simple.
Then, you need to specify the correct path by removing the previous android path, which may be the internal memory or SD card to match your directory tree
In the example in the image above, the playlist must be in the same directory as the songs (the folder structure) on android to work, as the player will search for the songs in the current directory symbolized by ". \"
For the playlist to work anywhere it will be run inside windows, you need to replace it with the full path, as in the image below

Write some text and save on my android phone

I'm trying to save some text on file (test.txt) on my android phone but unsuccessful. This is my code example:
procedure TDM.WriteLog(text:string);
var
myFile : TextFile;
begin
// Try to open the Test.txt file for writing to
AssignFile(myFile, System.IOUtils.TPath.Combine(System.IOUtils.tpath.getdocumentspath,'test.txt'));
ReWrite(myFile);
// Write a couple of well known words to this file
WriteLn(myFile, text + sLineBreak );
// Close the file
CloseFile(myFile);
end;
When i locate file on my phone i open file but it is empty. What I missed?
When you want to create a text file in Android (it's the same with iOS and macOS of course) you can use the WriteAllText class procedure that belongs to the TFile class. You have to add in the uses clause System.IOUtils.
TFile.WriteAllText(TPath.Combine(TPath.GetDocumentsPath, 'test.txt'), 'content');
When you want to store a text file, or in general data related to your app, it's recommended that you use the GetDocumentsPath. This is a modern way to create a text file but it's not the only one; consider also that you could:
Use a TStringlist and then call SaveToFile('path')
If you are using a memo it has the SaveToFile('path') function
Use the TStreamWriter class (and the StreamReader to read the content)

How to get path to file in android apk

My app depends on 3 different files. I already put the two larger ones as expansion files which seems to work fine. Now I don't know where to put the third one (small file). I tried the raw folder but actually I don't know what the path of the file is once it is in those folders.
I tried this here for the raw folder
myClassifier.loadModel( pathToExpansionFiles + "/File1",
pathToExpansionFiles + "/File2",
R.raw.File3);
However the return value of R.raw.File3 is an integer, but the function myClassifier expects a string that is the path to File3. Has anyone an idea how to do that?
I tried the raw folder but actually I don't know what the path of the file is once it is in those folders.
There is no path to it. That is a file on your development machine. It is not a file on the device. It is merely an entry inside your APK file.
but the function myClassifier expects a string that is the path to File3
Either switch to some library that works with an InputStream (from getResources().openRawResource()) or make a copy of the data to a local file using that InputStream yourself.

android data save directories

My application is mostly c++ (using NDK) so I use fopen, fwrite, etc. standard functions to create and game save files and write into them.
When I use fopen("game.sav", "wb"), it appears that it's being created at path
/data/user/10/com.my.game/files/game.sav.
My app is multi-user. So I want to have a separated folders where users store their save-files. And instead of the path above I'd like to have paths like
/data/user/10/com.my.game/files/user0/game.sav,
/data/user/10/com.my.game/files/user1/game.sav, etc
My app's frontend is in Java, and when new user is being registered, I want to create a folder /data/user/10/com.my.game/files/user0/. But I don't know how to do it, because
final File newDir = context.getDir("user0", Context.MODE_PRIVATE);
results in path being created at /data/user/10/com.my.game/app_user0 that's a different path.
It is possible to create folders at /data/user/10/com.my.game/files/ and how ?
Simple way to do it, this code you can change it suit many conditions. If you know that your path is different from what getFilesDir() gets you then you can create a File first of all by using a path that you know and the last 2 lines of code will still be same.
File file = this.getFilesDir(); // this will get you internal directory path
Log.d("BLA BLA", file.getAbsolutePath());
File newfile = new File(file.getAbsolutePath() + "/foo"); // foo is the directory 2 create
newfile.mkdir();
And if you know the path to "files" directory:
File newfile2 = new File("/data/data/com.example.stackoverflow/files" + "/foo2");
newfile2.mkdir();
Both code works.
Proof of Working:

strange characters at the end of directory in android

I have an issue with directory creation in Android. I use this code to create a directory if doesn't exist and then create a file under it.
dir=new File(Constants.TASK_DIRECTORY);
if(!dir.exists())
dir.mkdirs();
file=new File(dir, FILENAME);
file.createNewFile();
Sometimes it works fine, but sometimes when I check the folder from adb shell I see directories ending with 3 or more "|" characters. My directory name format is
"Abc_123-10.10.2000 ". What I see sometimes is exactly the same, but sometimes "Abc_123-10.10.2000|||" . I need to access the files under directories with the help of their name format but this situation makes it hard, any help would be appreciated.
I set TASK directory in this code
Constants.TASK_DIRECTORY=getFilesDir()+"/"+app.getUserName()+"-"+dt;
app is my application object
Edit: Solved this problem, it was because TASK_DIRECTORY was not properly set and contains "|" characters. But how can this be possible?
File can't store or save with some special character as below.
/\:?*"<>|

Categories

Resources