Unity, JSON file - Android problem, can't find my JSON file - android

I wrote a script that allows you to read and write to a json file. As long as I tried the script in unity editor, I didn't have any problems. But when I switched to installing the application on an Android device, I can't find my json file.
The json file is called "playerData.json" and has been placed in a folder called "playerData.json".
This is the code for the path of the json file (working on unity but not working on android)
path = Application.streamingAssetsPath + "/playerData.json";
I tried using this string of code to get the code to work on android as well, but there is no way to get it to work.
path = System.IO.Path.Combine(Application.streamingAssetsPath, "/playerData.json");
I only wrote the line concerning the path as my android device cannot find the JSON file. Thanks everyone for the help!

Application.streamingAssetsPath does not work on Android and WebGL
It is not possible to access the StreamingAssets folder on WebGL and Android platforms. No file access is available on WebGL. Android uses a compressed .apk file. These platforms return a URL. Use the UnityWebRequest class to access the Assets.
There is a workaround though as mentioned using UnityWebRequest, can find an example in this Unity Answer

Related

Get config.xml file of Cordova applications using Androguard

Is it possible to get the config.xml file that is generated by Cordova based applications using Adroguard?
I cannot find it under the /res folder and it is not listed in the output of get_files() method.
apktool, on the other hand, is able to get the config.xml from the apk that is use it on.
Since it is under res folder, You need to get it by unzipping the file to a temporary directory and then parse it using Axml parser. In get_files(), you must see "resources.arsc" file. The res files are part of that. You can do something like :
config_xml_path = os.path.join(<your apk unzipped path>, 'res', 'xml', 'config.xml')
with io.open(config_xml_path, 'rb') as axml_file:
parsed_axml_file = AXMLPrinter(axml_file.read())
axml_content = parsed_axml_file.get_buff().decode('utf-8)
parsed_axml = minidom.parseString(axml_content.encode('utf-8'))
You might get some errors if the config.xml is badly formatted but I am not including the solution to handle those case. I hope you will get an idea with the above example.

Package and read a file in android app just like in iOS

In my iOS project i kept a binary file and used NSBundle class to access that file.
How do i do that same thing in Android since recently i have started working on Android platform, i wanted to know how do i package and read a binary file in Android app.
I started doing it via native call, created a java wrapper and called a function in cpp file and in that function i am trying to read the binary file.
File *f = fopen("absolute file path on my laptop", "r")
Here f is always null whereas in iOS using NSBunle i am able to access the file in my iOS app
I passed and assetmanager object from java to NDK end.
then called on AAssetManager_fromJava and fetched AAssetManager object on jni layer and used that final object to read the asset.
Put the file at design time in assets directory.
At run time open an InputStream for the file in assets and read() its bytes.
You can all do in Java. Easy.

QPython - Reading a file

I have installed QPython in my Android mobile.
I written a statement in the QEdit to read a text file from the below path
/storage/emulated/0/com.hipipal.qpyplus/script3/File1.txt
I used the below statement
fob=open('/storage/emulated/0/com.hipipal.qpyplus/script3/File1.txt','r')
fob.read()
If I run the statement, it is throwing error as:
IOError:[Errno 2] No such file or directory: '/storage/emulated/0/com.hipipal.qpyplus/script3/File1.txt'
1|uo_a116#cancro:/ $
Is the above statement correct?
fob=open('File1.txt','r')
Is not working in version 1.0.4.
fout=open('File2.txt','w')
Was working on version 0.9.6, but is not working in 1.0.4.
The "error" is Read only file system.
It looks like restrictions in the (new 1.0.4) file system library. I post a mail to the editor, but no answer at this time.
For testing, try to write absolute path to your files pointing, for example, to sdcard (/sdcard/out.txt).
I had problems on this versions (>=1.0.4) because launch process of script changes and execution directory is not the same as script directory.
I had to change my scripts to point to absolute paths.
It was tested with qpython developer.
Check this link:
https://github.com/qpython-android/qpython.org/issues/48
You can also try as simple as:
fob=open('File1.txt','r')
fob.read()
Just if the script is in the same folder of the file.
You can change the current working directory to path with script before read file:
import os
os.chdir(os.path.dirname(os.path.abspath(__file__)))

Corona SDK: system.pathForFile returns nil

This code works in the simulator but not on my Android device:
local path = system.pathForFile("chinese_rules.db")
print("PATH:: " .. tostring( path ) )
When I run this code on my Galaxy S4 path returns nil.
My first thought was that it was some typo (case sensitivity) but I can't find any typo:
http://i59.tinypic.com/wlpu14.png
I can't find any reason why it should receive nil. This causes a problem as I can't load my database.
I have also tried this with the same result:
local path = system.pathForFile("chinese_rules.db", system.ResourceDirectory)
I have been able to load a path and load databases like this before.
Corona Build: 2013.2100 (2013.12.7)
Further reading the documentation I don't see that .db is a restricted file type:
Corona allows direct loading of images and audio files using the
appropriate APIs, but it has limited access to resource files on
Android using the file I/O APIs. Specifically, the following types can
not be read from the resources directory: .html, .htm., .3gp, .m4v,
.mp4,.png, .jpg, and .ttf.
http://docs.coronalabs.com/api/library/system/pathForFile.html
I found out the reason for the problem:
We are two that are working on this project and he had setup to use expansion files so two files was created (the main APK and the OBB expansion file) which I didn't notice and I only loaded the main APK file and I guess the database is in the OBB file. After setting not to use an expansion file the app works.
usesExpansionFile = false

Issue with Uncompressing the .7z file in the Android evn

Getting "system.entrypointnotfoundexception: loadlibrary" While trying to use SevenZipLib.dll to uncompress the .7z file containing media contents/file in the Android evn.
Context:
-The whole program is written in c# as a MONO Android Project. No Build/Deployment Error/warnings.
While running the apk, its throwing "system.entrypointnotfoundexception: loadlibrary".
-Also tested the same code as windows project (not mono) - uncompressing in the windows evn.
Assumptions for the issue:
7zip internally might be using COM components & Mono frame work is not supporting.
Question:
Has anyone come across similar issue? Please suggest some alternative dll/framework which can be used by my apk for uncompressing the .7z file.
Assuming that SevenZipLib.dll is the SevenZipLib Library on CodePlex, the problem is SevenZipLib\SevelZipLib\SevenZipArchive.cs:
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern SafeLibraryHandle LoadLibrary(
[MarshalAs(UnmanagedType.LPTStr)] string lpFileName);
The project contains numerous P/Invokes into kernel32.dll (LoadLibrary(), GetProcAddress(), FreeLibrary()), ole32.dll (PropVariantClear()), oleaut32.dll (SafeArrayCreateVector()), and more.
In short, this library is intimately tied to Windows, and isn't going to work on a non-Windows platform in any meaningful fashion, not without a lot of work.
If you need 7z support in a Mono for Android application, you'll need to look into a different library. It looks like the 7-zip SDK includes C# source for reading LZMA files that doesn't rely on P/Invoke, so perhaps that would work?

Categories

Resources