EDIT: I did some research and important for my question is this:
I use Intent object to get file path from user. He uses some random file explorer. The one I installed to emulator returns path like this:
fileName = "file:///mnt/sdcard/Alien/PlayersBase.btb"
This doesn't work.
If I was accessing the same file programatically, knowing where file is, I would do it this way:
string str = System.IO.Path.Combine(Android.OS.Environment.ExternalStorageDirectory.ToString(), "Alien/PlayersBase.btb");
Now
str = "/mnt/sdcard/Alien/PlayersBase.btb"
The second path is different and works perfectly.
So my question should be probably more like this: What can I expect to be returned by the Intent object and why string returned by the first file explorer I found on google returns something that doesn't work?
First version of question
My target is to allow user to select a file in android and I want to copy this file to local folder of my application.
From this question I took the third answer to allow user to select file on both Samsung and non Samsung devices. The code below seems to work fine, android gives me choice which explorer do I want to use, I can browse and select file. Just to be complete I post the code here:
Activity mainActivity = context as Activity;
string minmeType="*/*";
Intent intent = new Intent(Intent.ActionGetContent);
intent.SetType(minmeType);
intent.AddCategory(Intent.CategoryOpenable);
//special intent for Samsung file manager
Intent sIntent = new Intent("com.sec.android.app.myfiles.PICK_DATA");
// if you want any file type, you can skip next line
sIntent.PutExtra("CONTENT_TYPE", minmeType);
sIntent.AddCategory(Intent.CategoryDefault);
Intent chooserIntent;
if (mainActivity.PackageManager.ResolveActivity(sIntent, 0) != null)
{
// it is device with samsung file manager
chooserIntent = Intent.CreateChooser(sIntent, "Open file");
chooserIntent.PutExtra(Intent.ExtraInitialIntents, new Intent[] { intent });
}
else
{
chooserIntent = Intent.CreateChooser(intent, "Open file");
}
try
{
mainActivity.StartActivityForResult(intent, 3);
}
catch
{
SendMessageToUser("No suitable File Manager was found.");
}
Then I put this code to my activity
protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if (requestCode==3 && resultCode == Result.Ok) //pick file
OnFileSelected(data.DataString);
}
And I try to work with the file here
public void OnFileSelected(string fileName)
{
try
{
Stream streamSource = new FileStream(fileName, FileMode.Open, FileAccess.Read);
...
}
catch (Exception e)
{
string message = e.ToString();
}
}
Initializing streamSource however throws an exception:
fileName = "file:///mnt/sdcard/Alien/PlayersBase.btb"
and exception is
"System.IO.DirectoryNotFoundException: Could not find a part of the path "/file:///mnt/sdcard/Alien/PlayersBase.btb" ................."
I would understand if I didn't have rights for some folders but why does it say the folder doesn't exist when it is the exact file I selected in file explorer?