i'm having a small issue with Xamarin.Forms
This works -
Java.IO.File path = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDcim);
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(System.IO.Path.Combine(path.AbsolutePath, "dsdsd.txt"), false))
{
sw.WriteLine("yoyoyoyoy");
sw.Close();
}
However , using the XF Folder options as in "Working With Files" Guide ..The file is not created
var fPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
string filePath = Path.Combine(fpath, "dsdsd.txt");
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(filePath, false))
{
sw.WriteLine("yoyoyoyoy");
sw.Close();
}
Any idea what is causing this issue ?
I would prefer to use my App folder instead of a public one .
I did a test on the paths :
With Xamarin Environment :
/data/data/TestApp.Droid/files/dsdsd.txt (Does not work)
With Android Path :
/storage/emulated/0/DCIM/dsdsd.txt (works)
My Actual app Folder path (When i browse using pc) :
\Phone\Android\data\TestApp.Droid\files
Related
how load local html on air sdk harman, i use StageWebView on AS3, if i run on its normal view, but if i make apk and instal on android phone, its error. Application cannot showing local html. I use air SDK harman 33.1.1.554.
this my code :
var file:File = File.applicationDirectory.resolvePath("html");
var destinasi:File = File.applicationStorageDirectory;
file.copyTo(destinasi, true);
var hasil:String = "file://" + destinasi.resolvePath("index.html").nativePath;
isiMenu4.loadURL(hasil);
is there issue about security on air sdk by harman? how to skip that?
This might be an issue where you don't have Android permission to write to local storage.
Try creating the directory first and see if that throws any errors.
_file = File.applicationStorageDirectory.resolvePath("html");
trace("html.url", _file.url);
trace("html.nativePath", _file.nativePath);
if (!_file.exists) {
_file.createDirectory();
//make storage directory non backupable
_file.preventBackup = true;
}
For a robot simulation I use a csv file with data. I read data as follows:
string dbPath = ""; string realPath; // Android string oriPath = System.IO.Path.Combine(Application.streamingAssetsPath, "Data.csv");
// Android only use WWW to read file
WWW reader = new WWW(oriPath);
while (!reader.isDone) { }
realPath = Application.persistentDataPath + "/db";
System.IO.File.WriteAllBytes(realPath, reader.bytes);
dbPath = realPath;
using (StreamReader sr = new StreamReader(dbPath))
{...}
In the Unity Editor version the simulation works as expected but on Android the arm ofthe robot moves in a strange way as you can see in the videos. I compared the two db files (on the Windows and Android paths) and the content is identical. What could be the reason of the strange movemenz?
Editor https://streamable.com/7z4qob Android https://streamable.com/rv4nm2
Thank you!
To get StreamingAssets WWW you need to add prefix jar:file:///
To get Application.persistentDataPath you need to add prefix file:///
If files are identical then problem is not in getFile code, why did you provide it?
My application records data from a patient monitor and stores it a '*.dat' file in 'Environment.SpecialFolder.Personal'.
The app also allows the user to 'play back' previously acquired recordings.
I wish to include a few sample 'dat' files within the apk. Is it possible to include such files as 'assets' in the project and have them copied to 'Environment.SpecialFolder.Personal' at the time of installation?
Copy the database into Assets folder.
And set the Build Action as AndroidAssect.
You could use the following code to copy the file from Assects folder to Android Application folder.
// Android application default folder.
var appFolder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
var dbFile = Path.Combine(appFolder, "database.db3");
// Check if the file already exists.
if (!File.Exists(dbFile))
{
using (FileStream writeStream = new FileStream(dbFile, FileMode.OpenOrCreate, FileAccess.Write))
{
// Assets is comming from the current context.
await Assets.Open(databaseName).CopyToAsync(writeStream);
}
}
Download the source file from the link below.
https://github.com/damienaicheh/CopyAssetsProject
Updated:
You could use the code below.
var assetName = "someasset.jpg";
var outputName = Path.Combine(Android.OS.Environment.DirectoryDownloads,
assetName);
using (var assetStream = context.Assets.Open(assetName))
using (var fileStream = new FileStream(outputName, FileMode.CreateNew))
{
await assetStream.CopyToAsync(fileStream);
}
If your device is higher than Android6.0, you need runtime permission.
https://learn.microsoft.com/en-us/samples/xamarin/monodroid-samples/android-m-runtimepermissions/
For more details, please refer to the link below.
How to save file from assets on my device? Xamarin
I want to import pdf in my xamarin android project.
I want to have a pdf in my application folder at the installation of the application.
Bu when i try to add this pdf in android ressources and try to open it, i get a path error.
i save my pdf in draxable ressource and call path like : #drawable/pdfname or pdfname. the two solution don t work.
Best regards,
ok, i put it in the asset folder. I want to send email with attachement. I try this but the app said me the file is empty. var file = new Java.IO.File("Assets/CV_2017.pdf"); var uri = Android.Net.Uri.FromFile(file); i set my ressource as "build as android asset".
To get the url of the file in assets folder, you can code for example like this:
var file = new File(Android.Net.Uri.Parse("file:///android_asset/CV_2017.pdf").ToString());
if (file.Exists())
{
var uri = file.AbsolutePath;
}
If you want to access the stream of the file under assets, you can use AssetManager and code for example like this:
AssetManager assets = this.Assets;
using (StreamReader sr = new StreamReader (assets.Open ("CV_2017.pdf")))
{
//TODO:
}
I've created a new file using XmlSerializer and StreamWriter to persist data. From the test I did with my app, storing and restoring data using this method is working. Just for curiosity, I've tried to find the file created in the Android File System, without success. I've tried to find it with an Android app (ES File Explorer) and a desktop app (on Mac, Android File Transfer). In both case, I was unable to find the created file.
XmlSerializer xmlSerializer = new XmlSerializer (typeof(GAData));
TextWriter writer = new StreamWriter (FilePath);
xmlSerializer.Serialize (writer, this);
writer.Close ();
Console.WriteLine ("Data saved");
Where FilePath is define here :
// Determining the path
var documentsPath = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
FilePath = Path.Combine (documentsPath, "AppData.txt");
Console.WriteLine (Path.GetFullPath (FilePath));
The last Console.WriteLine is logging : /data/data/com.domain.myapp/files/AppData.txt
The exact same code is working like a charm on iOS, and I can see the file in the File System using an app on my Mac. Why can I find it in the Android File System? Or, where is it saved if it's somewhere else?
/data/data/com.domain.myapp/files/AppData.txt
That is private internal memory. Private as only your app has access.
Your app can reach it using getFilesDir().
Other app like ES File Explorer have no access.
You should be using FilesDir from the ContextWrapper
Path.Combine(FilesDir.Path , "MyFile.text");
Example, copies a file that is in the assets folder to the local file system
if (!File.Exists(Path.Combine(FilesDir.Path , "myFile.Text")))
{
using (var asset = Assets.Open("TextToCopyTo.txt"))
{
using (var dest = File.Create(Path.Combine(FilesDir.Path , "MyFile.text")))
{
asset.CopyTo(dest);
}
}
}