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

2

5

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.

Stockist answered 28/3, 2012 at 11:54 Comment(5)
Looks like you are missing java and Mono code, you can use one of the two, but not both togetherPhototopography
But then how do I port above line of code?Stockist
You have to convert all of your Java coffee to Mono. Is there any reason that you'd want write in Mono if you want to run the program on Android?Phototopography
@Phototopography Monodroid allows you to write your Android applications in C# and reuse existing .NET code, that is one reason for using the product.Alissaalistair
@Phototopography you'r assuming an existing Java code base. Mono for Android is targeted at those without an existing Java code base but an existing C# code base that they'd like to reuse.Animato
A
9

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);
Animato answered 28/3, 2012 at 17:38 Comment(0)
B
0

You are trying to convert System.IO.Stream to Java.IO.InputStream which is not allowed.. both are different environments.

What you want to achieve here can be done using System.IO.Stream, so no need to convert!!

            System.IO.Stream input = context.Assets.Open(FILENAME);
            Java.IO.FileOutputStream output = new Java.IO.FileOutputStream(file);
            byte[] buffer = new byte[1024];
            int size;
            while ((size = input.Read(buffer, 0, buffer.Length)) > 0)
            {
                output.Write(buffer, 0, size);
            }
            input.Close();
            output.Close();
Brunel answered 7/9, 2019 at 17:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.