I've been trying to get this to work for the last couple of hours and it's driving me nuts.
I have a method set up to save out data, which works flawlessly on PC, however, on Android it will not write the file. Here's my code:
public void Save()
{
string data = buildJson();
File.WriteAllText(Application.persistentDataPath + "/playerSave.json", data);
Debug.Log("File Saved: " + Application.persistentDataPath + "/playerSave.json");
}
Not sure why it's not working. From what I can tell reading other threads, using the Application.persistantDataPath is the correct thing to do.
Any help?
If WriteAllText failed, an exception would be thrown. Why not try catch the exception and check what the error is?
Solved.
My problem was that the method wasn't even being called correctly.
I had created a singleton gameobject which was holding the script with my save method in it, but I wasn't using a reference to save it, which meant it only worked when I was calling the save method in the same scene the gameobject was created. (Which was the case when I was skipping my menu scene in the Unity editor, but not when I was testing it for real in the app)
On android File.WriteAllText(Application.persistentDataPath + "/playerSave.json", data) will write data on your sd card you need to add permission android.permission.WRITE_EXTERNAL_STORAGE in your manifest file. The default manifest file created by unity do not add this permission.
Related
I'm storing user data in ApplicationData folder. Its path is obtained with :
userDataPath = System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData), "userData");
This variable is equal to /data/user/0/APPNAME/files/.config/userData.
Each time I rebuild the project, if I delete the userData file with File.Delete(userDataPath), I can successfully create the file, write and read to it several times. I can indeed check the created file in /data/data/APPNAME/files/.config/userData.
I check in /data/data/.../.config/userData and not in /data/user/.../.config/userData because apparently the latter is a symlink to the former, so it should be equivalent ? Moreother I don't have access to /data/user/.../.config/userData.
The problem is that if I rebuild the app without deleting the file, I got an unhandled exception at the following Deserialization (which worked fine before) :
if (File.Exists(userDataPath))
{
Stream reader = new FileStream(userDataPath, FileMode.Open);
Console.WriteLine(userDataPath);
userData = (UserData)serializer.Deserialize(reader); // ERROR HERE
reader.Close();
}
It is very strange because the file located at /data/data/APPNAME/files/.config/userData does not exist but since File.Exists(userDataPath) is true, the file located at /data/user/0/APPNAME/files/.config/userData does exist.
So how can this be explained and is this the correct way to store data in ApplicationData folder ?
After switching to another SpecialFolder (LocalApplicationData), I can't reproduce the unhandeld exception anymore (even when switching back to ApplicationData).
I'll keep this post updated if it ever happens again.
So, I'm currently trying to read an Audio file I just saved on the App's directory (Android) through the cordova file-plugin, but I keep getting the same error code 5, which stands for "ENCODING_ERR".
This is how I create the file and start recording
start() {
this.filename = this.file.externalDataDirectory.replace(/file:\/\//g, '');
this.mediaobject = this.media.create(this.filename + 'audioprofile' + '.3gp');
this.mediaobject.startRecord();
}
This is how I stop recording and save the file
stop() {
this.mediaobject.stopRecord();
this.mediaobject.release();
...
And this is where I'm stuck: right after saving it, I need to have it as a String, so I'm try to read it ( alert(content) should show me that string)
stop() {
this.mediaobject.stopRecord();
this.mediaobject.release();
this.storage.get("uid").then((id) => {
try{
this.file.readAsDataURL(this.filename,'audioprofile'+'.3gp').then((filecontent)=>{
alert(filecontent);
},(err)=>{
alert(err.code);
})
} `
After some research I found out it PROBABLY means I'm not giving the right path for it, but I've tried everything, any combinations of 'filename' and 'filepath' were made, even adding the prefix removed on start().
I want to know if someone managed to read a file with this cordova plugin and if you did, please help me out.
Thanks in advance, this is my first post here \o/ (although I've always used the website, love u guys).
i had the same problem. I solved it giving this path:
this.media.create(this.file.externalDataDirectory + this.nameFile);
I dont know why but this.file.readAsDataURL cant read the file if u save it deleting /file:
Remember change the path in all your methods.
Well i managed to do this with the File-Path Plugin, it resolves the Path for your file in a way the File Plugin understands and is able to reach the file, then you just have to manipulate it the way you want.
I am looking for a solution regarding a repeating log print that is caused by calling
BitmapFactory.decodeFile.
In My app i have a ListView that is being redrawn by a timer every second.
The ListView has an ImageView that gets is image source from the local storage, (not from the network)
The image is stored in :
filePath = /data/data/com.xxx.testlib/files/b22a1a294fd6e5ad3ea3d25b63c4c735.jpg
I am using the following code to redraw the image and it is working fine. with out exception.
try
{
File filePath = context.getFileStreamPath(imageName);
if(filePath.exists()){
bMap = BitmapFactory.decodeFile(filePath.getPath());
}
}catch (Exception e)
{
e.printStackTrace();
}
But when preforming the following line :
bMap = BitmapFactory.decodeFile(filePath.getPath());
I get a print in the log as follow:
03-07 09:55:29.100: I/System.out(32663): Not a DRM File, opening notmally
03-07 09:55:29.105: I/System.out(32663): buffer returned
....
How can i get read from the printing to the log.
Thank you
lior
Edit
Also it lags the phone whenever this operation is performed. And this reduced performance is noticeable specially when the phone is Waked up and we return to activity with this code.
Its more than a year for OP and still no answer is found. If anyone has found solution then please post it.
Thank you.
DRM stands for Digital Rights Management. It's normally a special keys used by owners of content to make sure that your device is authorized to view/play the content. iTunes was notorious for this for ages.
All it's doing is letting you know that the material you are opening is not DRM protected, and therefore can be opened normally.
Hope, this might help you.
I also got the same exception when i tried to save the image captured by camera directly to : /data/data/com.xxx.testlib/images/b22a1a294fd6e5ad3ea3d25b63c4c735.jpg.
Then i first saved the image to default location used by camera and the copied it to : /data/data/com.xxx.testlib/images/b22a1a294fd6e5ad3ea3d25b63c4c735.jpg.
and now "Not a DRM File, opening notmally" is removed from the log and saved the image successfully.
Conclussion : folder :- "/data/data/com.xxx.testlib/" is private and can be accessible from inside the application only.
Maybe it's a permission error.
Do you have added the right permission in your Manifest ?
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I'm trying to delete a file that I earlier created in my android app.
The problem I'm having is that the file won't go away. Even though everything seems to work.
I've looked at several post here on stackoverflow, but still not solution. The garbage collections was one of the hints I've found.
System.gc();
System.out.println("Exists: "+file.exists());
System.out.println("Read: "+file.canRead());
System.out.println("Write: "+file.canWrite());
System.out.println("Deleting: " + file);
boolean r = file.delete();
System.out.println("Result of deletion: "+r);
System.gc();
And the result in the log
Exists: true
Read: true
Write: true
Deleting: data/data/no.ntnu.kpro.app/files/kprothales/XOMessage/8
Result of deletion: true
Does anyone have any idea as to why it isn't removed?
EDIT:
Lucifer: Yeah, I have set WRITE_EXTERNAL_STORAGE permission in the manifest.
ShineDown: No, it is just a file without an extension. For now it is containing xml, but this is going to change over time, hence why I have not called it .xml. Could this be a problem?
chintan khetiya: I believe this line is allready included in the code above.
check the answer here:
Android: how to delete internal image file
which is basically suggesting to call deleteFile:
if(activity.deleteFile(imageName))
Log.i(TAG, "Image deleted.");
I've researched this problem for a while now and I've only found really complicated answers so I'm very confused. Keep in mind that I'm not an expert programmer so don't expect me to know a ton about this!
All I want to do is print a new line of characters to a text file located in the downloads folder of an SD card in Android. I set up my emulator to have an SD card and placed the text file in the downloads folder. This piece of code is for a database class that will access a text file in an SD card to read the data. I know that the class works outside of Android so assume that all of the methods are working as they should to read the data!
I get an IOException when I run this method in another class:
public void addRecordToDataBase(ChildRecord c) throws IOException
{
FileWriter outFile = new FileWriter("/mnt/sdcard/download/database.txt");
PrintWriter out = new PrintWriter(outFile);
out.println(c.printToDataBase());
out.close();
}
The weird thing is I can read from the database just fine in other methods using that same path; no problems there. I just can't write to it. I've read somewhere that you can use "regular Java methods" to write to an SD card in Android without those crazy "OutputStream" things all over the place. Is this true? I debugged this thing and found out that the line of code that is throwing the exception is right here:
FileWriter outFile = new FileWriter("/mnt/sdcard/download/database.txt");
If anyone has any idea why I'm geting this IOException, I would be really grateful! I did try all the crazy methods that Android wants to use but I think I got lost in it so I just reverted back to what I knew how to do.
Thank you so much!
My guess is you can't access such directory with writing permissions, at least in that manner.
Did you take a look at http://developer.android.com/guide/topics/data/data-storage.html#filesExternal?
That reference and this one (http://developer.android.com/reference/android/content/Context.html#getExternalFilesDir(java.lang.String)), explaining how getExternalFilesDir works, may be of help to you!
Have you declared the WRITE_EXTERNAL_PERMISSION permission in your apps manifest file?