John
John

Reputation: 430

Handling intent callback Xamarin.forms

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.

Upvotes: 0

Views: 521

Answers (1)

Rohit Vipin Mathews
Rohit Vipin Mathews

Reputation: 11787

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(); 
    } 
}

Upvotes: 1

Related Questions