How to Open and Read text files on Android in C# - c#

The code I have now is:
Intent intent = new Intent();
intent.SetType("text/plain");
intent.SetAction(Intent.ActionGetContent);
StartActivityForResult(Intent.CreateChooser(intent, "Select file"), 1);
string str = System.Environment.GetFolderPath(?????);
That last line of course doesn't work. I'd like the user to be able to browse folders on the device and open one of them. This is simple in Windows but Android is proving difficult.
1/3/2023:
This issue is still unsolved. Coming from Windows and C# this is all trivial. But Android, not.
I have tried these:
var documents = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
var documents = Path.Combine(Xamarin.Essentials.FileSystem.AppDataDirectory);
var documents = System.Environment.CurrentDirectorygetExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
WIth some I can save the file from my app on Android but I cannot find them (non-programmatically).
When I save files in FXTextEdit (on Android), I save them to folders I can easily access. Why not progammatically?
I am coding in C#.
Thanks.

Related

UIDocumentInteractionController always creates a copy

I'm currently developing a Xamarin.iOS App that gets a document from a web service that should then be edited offline on the tablet after being downloaded to the internal storage.
The most common answer was to use the UIDocumentInteractionController. However if I use the UIDocumentInteractionController then I can only create a copy of my original file and open this copy. To get it back into my app I have to make the user select the document from the 'UIDocumentPickerViewController'.
Is there a better way to make the `UIDocumentInteractionController' not create and open a copy of the original, or to at least get the url from the new documentcopy?
Code to open the file:
public void OpenFile()
{
var url = NSUrl.FromFilename(FilePath);
var controller = new UIDocumentInteractionController();
controller.Url = url;
controller.PresentOpenInMenu(table.Frame, table, true);
}
If that is not Possible: Are there different tools or controlls i could use to open and edit a MS-Office file directly?

Can't find path on android

I have been searching for almost a day now and this matter is driving me crazy.
I am trying to open a connection to a SQLite DB I push to the device from windows.
The database now is inside this path:
Phone/Android/data/MyApp.Droid/files/MyDB.db3
which can be read without 'Phone/Android' I think.
When I try to make a connection to the DB I can't seem to find the path.
Here's my code for retrieving the path and creating the connection.
public SQLite.SQLiteConnection GetConnection()
{
var sqliteFile = "MyDB.db3";
var dataPath = global::Android.OS.Environment.DataDirectory.AbsolutePath;
var dataPathExtension = #"MyApp.Droid/files";
var destination = Path.Combine(dataPath, dataPathExtension, sqliteFile);
//this outputs the following: /data/MyApp.Droid/files/MyDB.db3
//When I check my phone this is exactly when I can find the file.
return new SQLite.SQliteConnection(destination);
//It can't find the path/file.
}
The DB needs to be in a location I can access from windows without rooting the device.
Can anyone explain to me why it can not find the path/file and if possible tell me how I can read the location?
Note: I cannot access any 'Environment.SpecialFolder' from windows it seems as this gives me a path like: data/user/0/MyApp.Droid/files/
Yours,
I use this implementation:
public SQLiteAsyncConnection GetConnection()
{
var fileName = "DatabaseName.db3";
var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
var path = Path.Combine(documentsPath, fileName);
var connection = new SQLiteAsyncConnection(path);
return connection;
}
Then I'll grab the .db3 file using Android Device Monitor and the File Explorer inside. You can then open that .db3 file with http://sqlitebrowser.org/ or another browser that supports SQLite. Please note: It's easiest on an Emulator as pulling the files from a physical device can be quite cumbersome without root.
Open Tools -> Android -> Android Device Monitor:
Run your Application in Debug mode
Select your Device in the Devices section:
Pull your .db3 file from data/data/com.mypackage.name/files via the Save Icon:
Open the .db3 file in a SQLite browser

Open downloaded epub files in WebView

I google a lot over this and still can't found a way to make it work
My app downloads epubs from many sources and then I save them to the local Storage (and unzip them)
I can read the extracted files with no problems with Storage commands but just can't to open it on WebView control
I tried all this:
The real path: C:\Data\Users\DefApps\APPDATA\Local\Packages\xxx\LocalState\***
Uri localUri = new Uri("ms-appx-web:///***/OEBPS/04_CL_CH.01.xhtml");
Uri localUri = new Uri("ms-appdata:///***/OEBPS/04_CL_CH.01.xhtml");
Uri localUri = new Uri("file:///***/OEBPS/04_CL_CH.01.xhtml");
Uri localUri = new Uri("file:///LocalState/***/OEBPS/04_CL_CH.01.xhtml");
Uri localUri = new Uri("file:///C://Data//Users//DefApps//APPDATA//Local//Packages//xxx//LocalState//***//OEBPS//04_CL_CH.01.xhtml");
// two ways
WebView1.Navigate(localUri);
WebView1.Source = localUri;
// this works, but as is an epub file, so need lot of files and styles
var XHTML = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync("***\\OEBPS\\04_CL_CH.01.xhtml");
WebView1.NavigateToString(await Windows.Storage.FileIO.ReadTextAsync(XHTML));
This is a C# Windows Phone universal app, and I'm using VS 2013 Express
Take a look at http://blogs.windows.com/buildingapps/2013/07/17/whats-new-in-webview-in-windows-8-1/, it says:
ms-appdata:///local/TopLevelDirectory/file for files from the local
state
ms-appdata:///temp/TopLevelDirectory/file for files from the
app’s temporary state folder

translate android java to xamarin c#

I'm trying to make an android app with a webview which can upload images to a html page.
I think i found a solution here: Android ACTION_IMAGE_CAPTURE Intent
but i'm having trouble translating everything to Xamarin c#, any can help me here? the code i'm interestet in is:
File imageDirectory = new File("/sdcard/signifio");
String path = imageDirectory.toString().toLowerCase();
String name = imageDirectory.getName().toLowerCase();
ContentValues values = new ContentValues();
values.put(Media.TITLE, "Image");
values.put(Images.Media.BUCKET_ID, path.hashCode());
values.put(Images.Media.BUCKET_DISPLAY_NAME,name);
values.put(Images.Media.MIME_TYPE, "image/jpeg");
values.put(Media.DESCRIPTION, "Image capture by camera");
values.put("_data", "/sdcard/signifio/1111.jpg");
uri = getContentResolver().insert( Media.EXTERNAL_CONTENT_URI , values);
Intent i = new Intent("android.media.action.IMAGE_CAPTURE");
i.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(i, 0);
more specific it's what classes i have to import to find Media.TITLE, Images.Media.BUCKET_ID.... and so forth.
Try Android.Provider.MediaStore
If they aren't in their you could use the actual strings instead, see the Android documentation
Here's the call to getContentResolver Xamarin style:
this.ContentResolver.Insert(Android.Provider.MediaStore.Images.Media.ExternalContentUri, values);
For the location of the file use:
Environment.GetExternalStoragePublicDirectory(Environment.DirectoryPictures), "CameraAppDemo");
There is a recipe for using a camera intent in the Xamarin documentation.

Save PPS/PPT files as SWF using C#

I want to build a C# application that save a pps\ppt file as SWF.
For saving pps\ppt as image I use this code:
Application apps = new Application();
Presentations pps = apps.Presentations;
foreach (string file in Directory.GetFiles("[here is a path]", "*.ppt"))
{
try
{
Presentation p = pps.Open(file, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse);
p.SaveAs("[here is a path]" + p.Name, PpSaveAsFileType.ppSaveAsJPG); //It saves the slides as images in a folder.
Console.WriteLine(id.ToString() + ". " + p.Name);
p.Close();
id++;
}
catch{ }
}
So, how can I save the pps\ppt files from a folder as SWF?
There isn't this: PpSaveAsFileType.ppSaveAsSWF
No you can't using PowerPoint API.
To convert PowerPoint files to Flash you have look for software vendors that sell specific API to do that.
Anyway, not all PowerPoint objects/features are supported so you could lose some kind of contents.
Of course it can be done if you develop an application by your self but you need strong knoledge of PowerPoint API and Flash accessing pptx (openxml) files from Flash... it's a job.
You need to convert PPT files with some third-party software that has a C# API. I use print2flash for this purpose. The core conversion code I use is quite simple if I omit some options from my real C# application
Print2Flash4.Server2 serv = new Print2Flash4.Server2();
Print2Flash4.Profile2 prof = new Print2Flash4.Profile2();
prof.DocumentType = Print2Flash4.DOCUMENT_TYPE.FLASH;
serv.ConvertFile(inputfile, swf_file, prof, null, null);
To use this code, you need to get Interop.Print2Flash4.dll as well. You may get the dll and further instructions from the sdk available for download from http://print2flash.com/download.php

Categories