Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot implicitly convert type 'System.IO.Stream' to 'Java.IO.InputStream'

I referred some similar questions on SO but none of them deals with IO.

I had used the same code in java when I used Eclipse. That time it worked.

But now I try to use this code in Mono for Android (C#), it doesn't work.

I'm trying to run this code to create an InputStream:

InputStream myInput =ctx.Assets.Open(DATABASE_NAME + ".db");

But it is giving me compile-time error : Cannot implicitly convert type 'System.IO.Stream' to 'Java.IO.InputStream'

There is a direct function to copy a file from assets to device memory but that requires source and destination path.

How do I get the source Path???

As I'm absolute beginner to Mono for Android, any help appreciated.

like image 520
GAMA Avatar asked Mar 28 '12 11:03

GAMA


1 Answers

Mono for Android translates some Java constructs into "equivalent" .NET constructs to ease code sharing between .NET-like platforms. As part of this, java.io.InputStream and java.io.OutputStream are mapped to System.IO.Stream, hence the compiler errors.

Is there anything you require that exists on InputStream that doesn't exist on System.IO.Stream?

There is a direct function to copy a file from assets to device memory but that requires source and destination path.

I have no idea what InputStream method you're referring to here. You can use Stream.CopyTo(Stream) to do that:

Stream asset = context.Assets.Open(DATABASE_NAME + ".db");
string dbPath = System.IO.Path.Combine(
        System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal),
        "YourFile.xml");
using (var dest = System.IO.File.OpenWrite(destPath))
    asset.CopyTo(dest);
like image 58
jonp Avatar answered Sep 20 '22 22:09

jonp