I am developing an app in Android and there only one module is from Unity. I am downloading all data from server and save it in path with the method returns below.
getFilesDir().getPath()
Now in Unity, I want to get a json file from the directory and load the image in unity. But I am unable to get /data/data/PACKAGE_NAME from unity. Instead of that I am getting /Storage/Emulated/0/Android/data.I am using Application.persistentdata to get Android path. How to get /data/data path in Android. Please help me to resolve this. I am trying this for more than a week.
I am currently working on something exactly like this and for me, it's working properly. The steps are:
Get the image path. The Application.persistentdata is exactly what you need.
Know beforehand the name of the file/image you want to access with its extension also (for e.g. - fish.png / manifest.json).
Add the file/image name to the image path right before accessing it through WWW call.
These steps will suit your need. Let me know if you get any problem in any of the aforementioned steps.
If you know the exact path that should be read, why don't you read the file with normal methods?
using System;
StreamReader sr = new StreamReader("data/data/.../a.txt");
string content = sr.ReadToEnd();
sr.Close();
However if you know the file's path in only Java side, then you can comminicate between Java and C# codes using UnitySendMessage, or AndroidJavaObject etc..
Related
I've got c++ code in which I try to get a file from a directory on an android device. I've tried different ways to set the path which I pass to the fopen() function like:
/Android/data/com.myapp/files/Blip.wav
There actually is this file. But I guess that this is not a proper way to write a path. (The example was obtained by the java code )
getContext().getApplicationContext().getFilesDir().getPath() + "/Blip.wav"
There actually is this file
Since I have never seen an Android device with an /Android directory, that is unlikely.
What would fit is if you are looking at /Android/data/com.myapp/files/Blip.wav in a desktop file manager, using a USB or similar connection. In that case, Android/data/com.myapp/files/Blip.wav is a relative path in external storage. Specifically, it maps to:
new File(getContext().getExternalFilesDir(), "Blip.wav")
Try using this.
File root=Environment.getExternalStorageDirectory();
File file=new File(root,"/PersonData/Blip.wav");
Here personData is the name of folder
After searching for solution, I have found from this link that it is possible to retrieve path like:
Using Resource Id
Syntax : android.resource://[package]/[resource_id]
Example : Uri.parse("android.resource://com.my.package/" + R.raw.mp3filename);
Which is exactly what I want. Problem is that, I'm using lolipop, and it does not work. When the package/resource is parsed, path from Uri.parse will be null. Is there other way around to write this, since in my Song class, I have only access to resourceId (R.raw.mp3filename).
Is it possible to get file path by using R.raw.mp3file
No, because it is not a file on the device. It is a file on the hard drive of your development machine. On the device, it is merely an entry in an APK file.
Either:
Do whatever you are trying to do some other way that does not involve a file path, or
Use openInputStream() on a Resources object (you can get one from any Context via getResources()), and use Java I/O to copy the contents of that resource to a local file, such as on internal storage
I am new to Xamarin.Forms, and want to save some files like CSV, PDF in the device using Export to CSV / PDF.
And to do that I need to access device directory and path. But using Xamarin.Forms, How can I access directory and path in Xamarin.Forms for iOS & Android?
Thanks in advance!
System.Environment.GetFolderPath cannot be used with PCL project.(But shared project,It can)
If you plan to use PCL path, You can use 'Dependency Service' Feature.
http://developer.xamarin.com/guides/cross-platform/xamarin-forms/dependency-service/
use Interface(on PCL) that inject from Concrete Class (From iOS/Android). (Sound Hard,But It's easy)
You could use System.Environment.GetFolderPath to help with things such as this.
i.e.:-
string strFolderPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
In Android this will return something along the lines of /data/data/MyProject/files
You can then just append your filename onto the end and then start creating and writing to the file etc.
I have a text file "test.txt" under the Resources, and a function "parseSth(char* str)" in the "util.cpp" to read the "test.txt" do something.
However, if I use like this parseSth("text.txt"), the ios and android all cannot find the file. And use parseSth(CCFileUtils::sharedFileUtils()->fullPathFromRelativePath("text.txt")) will work fine on ios, but cannot find the path.
Why? How I can do?
I suppose that your file is embedded into APK file on Android and thus doesn't have a path to it per-se.
Use the following function to extract data from the file (it automatically handles apk/sd card differences).
FileUtils::getInstance()->getDataFromFile(filename)
I am trying to load local HTML (that exists in the project's folder structure) in an instance of webStageView. While this works fine in windows and ipad/iphone - it fails to locate the files in Android (only tried it in 2.2)
File.applicationDirectory.url returns "app:"
File.applicationStorageDirectory.url returns "app-storage:"
File.applicationDirectory.nativePath returns an empty string, as specified in Adobe's documentation.
The problem is that the webview gives a "Web page not available, app:/test/index.html not found" error. Is there a way to get the full path, or to "force" the browser to understand that app: refers to a specific folder?
Thanks!
As Adobe help states:
The app: and app-storage: schemes are not supported.
On iOS your code works perfectly, but on Android, you have to achieve it with some hacky implementation like yours, because security issues related to StateWebView loading urls from those directories. Another solution is to copy your app: or app-storage: file into a temporary one and load that temp file url into StageWebView:
var htmlFile:File = File.applicationDirectory.resolvePath(url);
var workingFile:File = File.createTempFile();
htmlFile.copyTo(workingFile, true);
stageWebView.loadURL(workingFile.url);
Edit: Another solution could be to have your html content coded into an .as file and use StageWebView.loadString() method. Having in mind that you have to encode that string for Android devices.
I fix this problem a couple of days a go. The file: URL scheme refers to a file on the client machine. There is no hostname in the file: scheme; you just provide the path of the file. So, the file on your local machine would be file:///~User/2ndFile.html. Notice the three slashes; the hostname part of the URL is empty, so the slash at the beginning of the path immediately follows the double slash at the beginning of the URL. You will also need to expand the user's path; ~ does no expand in a file: URL. So you would need file:///home/User/2ndFile.html (on most Unixes), file:///Users/User/2ndFile.html (on Mac OS X), or file:///C:/Users/User/2ndFile.html (on Windows).
This solution work perfectly in my app.