I basically want to be able to implement a feature for an android app to open a text file and read the contents to a string variable. I can handle this using android and Java but with forms I don't know how to go about it. I'm guessing dependency injection but a little guidance would do.
Asked
Active
Viewed 477 times
0
-
I think App links are what you looking for? https://blog.xamarin.com/deep-link-content-with-xamarin-forms-url-navigation/ – Rohit Vipin Mathews Dec 22 '16 at 11:42
-
@Rohit not exactly. I just want to select a text file with my app and read the text file. – John Dec 22 '16 at 12:55
-
do you want to do it via file picker? – Rohit Vipin Mathews Dec 22 '16 at 14:43
-
@Rohit yes. Exactly – John Dec 22 '16 at 14:44
-
@Rohit sorry if the question had been phrased wrong – John Dec 22 '16 at 14:45
1 Answers
1
You can write your own file picker if required and use it via dependency service.
There is a plugin which does what you are asking for - FilePicker Plugin for Xamarin and Windows.
This can let you handle file handling from your pcl project. The code is available in GitHub if you want to slightly customize it.
Eg :
private async Task PickFilesCommandHandler()
{
var file = await CrossFilePicker.Current.PickFile();
var fileEntity = new FileEntity
{
FileName = file.FileName,
DataArray = file.DataArray
};
}
Full example in Github.
For byte array to string conversion have a look at this answer.
Or use the following method via MemoryStream
:
using (var ms = new MemoryStream(bytes))
{
using (var streamReader = new StreamReader(ms))
{
var str = streamReader.ReadToEnd();
}
}

Community
- 1
- 1

Rohit Vipin Mathews
- 11,629
- 15
- 57
- 112
-
-
Quick question, how do I get the text value if it is a txt file to string variable? – John Dec 23 '16 at 08:35
-
What you have is a byte array. Just convert the byte array to string. Updated the answer to include that as well. – Rohit Vipin Mathews Dec 23 '16 at 08:36
-
-
Would it be possible to filter the file to be selected, like image, video and stuff like that? – John Dec 23 '16 at 08:59
-
You can check the extension once it's picked. I'm not sure of the filter. – Rohit Vipin Mathews Dec 23 '16 at 09:05
-