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.

GAMA
  • 5,958
  • 14
  • 79
  • 126
  • Looks like you are missing java and Mono code, you can use one of the two, but not both together – Kai Mar 28 '12 at 12:39
  • But then how do I port above line of code? – GAMA Mar 28 '12 at 12:51
  • 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? – Kai Mar 28 '12 at 13:20
  • @Kai Monodroid allows you to write your Android applications in C# and reuse existing .NET code, that is one reason for using the product. – dmck Mar 28 '12 at 13:50
  • @Kai 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. – jonp Mar 28 '12 at 17:59

2 Answers2

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);
jonp
  • 13,512
  • 5
  • 45
  • 60
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();
Afzal Ali
  • 802
  • 3
  • 9
  • 18