Unity I/O with Android device - android

i can read and write file with this method for storing game progres.
filePath = Application.dataPath + "/";
filenam= "SavedGame";
extension = ".txt";
Lettura ----------------
var FRead = new File.OpenText(filePath + fileNam + extension);
for (var i : int= 0; i<N; i++) {
FRead.ReadLine();
}
Scrittura--------------------
var FWrite: StreamWriter = new StreamWriter(filePath + filenam + extension);
FWrite.WriteLine("AAAA");
A run time in the Unity editor all work perfectly.
But in Android device Application.dataPath seems dont work.
I tryed Application.persistentDataPath that work fine but in (Read Only),but i need place the SavedGame.txt manually in the patch.
I tried a fantomatic "jar:file://" + Application.dataPath + "!/assets/"; but dont work.
Note when i build the project in apk packege semms dont appear my SavedGame.txt,that
in the editor is in UnityProject\Assets .
Come posso risalire al percorso giusto del mio SavedGame.txt, posizionato in ad esempio in UnityProject\Assets nei device?
How i cand do this in a correct way read and write SavedGame.txt in android device?

On android you might not have direct write access to the dataPath of the project.
Try using Application.persistentDataPath instead. This is where save files are intended to be stored.
Change:
filePath = Application.dataPath + "/";
to
filePath = Application.persistentDataPath + "\\";

Related

Fixing data path not found error with SQLite

I've got the sqlite db working in the editor just fine. However when using it in Android, I get an error saying data path not found.
I believe I have the needed libsqlite.so files in the correct plugins/ android folder and the other DLLs that the editor looks for.
Here is my code:
string filepath = Application.dataPath + "/" + "Players.db"; //.db
if(!File.Exists(filepath))
{
// if it doesn't ->
// open StreamingAssets directory and load the db ->
try {
WWW loadDB = new WWW ("jar:file://" + Application.persistentDataPath + "!/Assets/" + "StreamingAssets/Players.db"); // .db this is the path to your StreamingAssets in android
while (!loadDB.isDone) {
} // CAREFUL here, for safety reasons you shouldn't let this while loop unattended, place a timer and error check
// then save to Application.persistentDataPath
File.WriteAllBytes (filepath, loadDB.bytes);
} catch (Exception ex) {
GameObject.Find ("Advice").GetComponent<Text> ().text = ex.Message;
}
}
You are using Application.DataPath to save files. In editor mode it refers to Assets folder but in android build it refers to APK package which is a compressed file and you have no access to it. So use Application.persistentDataPath instead.
string filepath = Application.persistentDataPath + "/" + "Players.db"; //.db

Unity 3D Android and File

I have a problem with Android and Unity 3D. I have a file read code. When I put my code on the computer, it works. However, my code does not work on Android (Mobile). How can I solve this problem? Thank you.
FileInfo theSourceFile = new FileInfo(filename);
if (System.IO.File.Exists(fname))
{
StreamReader reader = theSourceFile.OpenText();
string text = reader.ReadLine();
print(string);
}
EDIT updated code
string filename = "file.txt";
FileInfo theSourceFile = new FileInfo(filename);
filename = Application.persistentDataPath + "/"+filename;
System.IO.File.WriteAllText(filename,"Test");
if (System.IO.File.Exists(filename))
{
StreamReader reader = theSourceFile.OpenText();
string text = reader.ReadLine();
print(string);
}
You need to change your build settings for android Device.
Change Configuration >> write access to external (sd card).
if not, your app is pointing to internal path and you need root permission in your android device.
You must use Application.persistentDataPath on Android to be able to read anything.
Change that to string filename = Application.persistentDataPath + "/file.txt"; and your code should work fine.
Bear in mind that before the read function can work, you must write to the directory first. So file.txt must exist in Application.persistentDataPath first.
For example
string filename = Application.persistentDataPath + "/file.txt";
System.IO.File.WriteAllText(filename,"Test");
EDIT:
You new code is still not working because you had FileInfo theSourceFile = new FileInfo(filename); before filename = Application.persistentDataPath + "/"+filename;. This means that the file name is still not valid. Pay attention the order your script execute. After switching it, it worked on my Android. Below is the whole code.
string filename = "file.txt";
filename = Application.persistentDataPath + "/" + filename;
System.IO.FileInfo theSourceFile = new System.IO.FileInfo(filename);
System.IO.File.WriteAllText(filename, "Test");
if (System.IO.File.Exists(filename))
{
System.IO.StreamReader reader = theSourceFile.OpenText();
string text = reader.ReadLine();
print(text);
}

extracting frames from a mp4.file

i want to open a .mp4 file, so that i can access each frame.
I tried to do it using javacv, but it didnt work.(see code below)
Does anyone know why it didnt work or how else i could achieve my goal?
i tried to call
string path = Environment.getExternalStorageDirectory().getAbsolutePath()
+ File.separator + "Test";
opencv_videoio.VideoCapture cap = new opencv_videoio.VideoCapture(path + File.separator + "test.mp4");
but then cap.isOpend() returns false. I have set the permission WRITE_EXTERNAL_STORAGE(which includes READ)

android download manager - download file into previously created directory

I want to save the downloaded file into a custom folder previously created as :
String trainingDirectory = "swimmer" + File.separator + "trainings";
String trainingsPath = Environment.getExternalStorageDirectory().toString() + File.separator + trainingDirectory;
File trainingSubdirectory = new File(getFilesDir() + File.separator + trainingsPath );
trainingSubdirectory.mkdirs();
to store the downloaded file into this directory, I tried to follow the solution given : Set custom folder Android Download Manager
writing
request.setDestinationInExternalPublicDir ( "/trainings", "mydownloadedfile.mp4");
In this case , the download manager is creating a new 'training' directory , not using the one I created previously...
I tried also to use
request.setDestinationInExternalPublicDir ( "/swimmer/trainings", "mydownloadedfile.mp4");
but in this case an error is raised ( a path with separators is not accepted..)
where am I wrong ?
Use this:
String directoryPath = Environment.getExternalStorageDirectory() + "/swimmer/trainings/"
// ...
request.setDestinationUri(Uri.fromFile(new File(directoryPath + "fileName.ext")));

Can't write image in air for android

This code works great in a desktop air application but doesn't work at all in android. is this not possible yet in air for android? here's my code.
var theImage:BitmapData = new BitmapData(imageArea.width,imageArea.height);
theImage.draw(imageArea);
var jpgStream:ByteArray = theEncoder.encode(theImage);
var fileName:String = "pf_" + int(Math.random() * 10000) + ".jpg";
while(File.userDirectory.resolvePath("DCIM/Camera/" + fileName).exists)
fileName = "pf_" + int(Math.random() * 10000) + ".jpg";
trace(fileName);
var fl:File = File.userDirectory.resolvePath("DCIM/Camera/" + fileName);
var fs:FileStream = new FileStream();
trace(fl.url);
try{
fs.open(fl,FileMode.WRITE);
fs.writeBytes(jpgStream);
fs.close();
}catch(e:Error){
trace(e.message);
}
I get this trace on the fl.url
file:///mnt/sdcard/DCIM/Camera/pf_2570.jpg
image area is a flex group with several displayed children. Thanks
I didn't try to debug your code, but what you are doing should work. It is certainly possible to write to files with AIR on Android. Are you sure jpgStream has data?
Did you add the android permission to write content on SD card? ( or external storage )
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Categories

Resources