fao fao - 1 year ago 106
Android Question

How can I get the number of bytes availables with Xamarin Android

I want to use this method in order to get an estimated number of bytes that can be read in a JsonFile from the Asset.

For that I wrote this code:

string filename;
Stream mf = Context.Assets.Open(filename);

The problem is that I cannot have mf as an InputStream because Context.Assets.Open(filename); gives a System.IO.Stream.

And I need to define mf as an InputStream in order to be able to have the size such as:

int size= mf.Available();

I tried also:

InputStream mf = AssetManager.Open(filename);

But I have the error of "
An object reference is required for the property , method, or the non-static field "AssetManager.Open""

How can I get over this conflict?

Answer Source

If you want the actual Asset file size, there are two things you need to do:

1st: Set the file NOT to be compressed

Example, change extension to something unique, i.e. .utxt

Set those file extensions not to be compressed in your app package:

enter image description here

2nd: Use an asset file descriptor:

var fileFD = Assets.OpenFd("json.utxt");
Log.Debug("JSON", fileFD.Length.ToString());
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download