0

I am developing an app which enables the user to select a file and do processing on it after getting the path of it, I have written a code which gets me the path like this

 private void OpenFile()
        {
            Intent i = new Intent(Intent.ActionGetContent);
            i.SetType("application/zip");
            StartActivityForResult(i,0);
        }

In activity for result I am extracting the path as follows:

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            base.OnActivityResult(requestCode, resultCode, data);
            if (requestCode == 0)
            {


    if (resultCode == Result.Ok)
                    {
 string uri = data.DataString;
                    System.Uri myUri = new System.Uri(uri, System.UriKind.Absolute);
                    Android.Net.Uri uris =  Android.Net.Uri.FromParts(data.Scheme, myUri.LocalPath, myUri.Fragment);
                  // string a= myUri.LocalPath;

                  System.IO.Stream input=  ContentResolver.OpenInputStream(uris);

                    string uri = data.DataString;
                    ZipLogic.Unzip(uri);
                }
            }
        }

And the results are in such pattern:

content://com.android.externalstorage.documents/document/xxxx-83BB%3xxx%2Fxxx.zip

But this path when I try to access from returns DirectoryNotFound Exception I am unable to resolve how to open this path as a Stream.

Asim Khan
  • 572
  • 6
  • 34

1 Answers1

0

Luckily, I found the answer by watching closely the Intent data.

protected override void OnActivityResult(int requestCode, Result resultCode, Intent intent)
    {
        base.OnActivityResult(requestCode, resultCode, intent);
        if (requestCode == 0)
        {
            if (resultCode == Result.Ok)
            {
                string uri = data.DataString;
                //intent variable has a Field named Data which is the complete URI for the file. 
                Android.Net.Uri uris = Android.Net.Uri.FromParts(intent.Data.Scheme, intent.Data.SchemeSpecificPart, intent.Data.Fragment);
                System.IO.Stream input = ContentResolver.OpenInputStream(intent.Data);
                //related tasks
            }
        }
    }

Explanation: The selected file result has a field named Data in the Intent object, that is basically the Uri Invoker which tends to be a URI object I used it to get an input stream from ContentResolver.

Asim Khan
  • 572
  • 6
  • 34