I am currently trying to send an image over FTP on Android. I tried a lot of possible ways to accomplish that, and the same problem persists: I get an error telling that the path/directory/file doesn't exist.
My code right now is using the CameraSample from Xamarin and getting the picture taken from the camera and trying to send it over FTP. When I try to do that, I receive the error:
ENOENT (No such file or directory)
Here is the code:
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
// make it available in the gallery
Intent mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile);
Uri contentUri = Uri.FromFile(_file);
System.Console.WriteLine (contentUri.ToString ());
mediaScanIntent.SetData(contentUri);
SendBroadcast(mediaScanIntent);
// display in ImageView. We will resize the bitmap to fit the display
// Loading the full sized image will consume to much memory
// and cause the application to crash.
int height = _imageView.Height;
int width = Resources.DisplayMetrics.WidthPixels;
using (Bitmap bitmap = _file.Path.LoadAndResizeBitmap(width, height))
{
_imageView.RecycleBitmap ();
_imageView.SetImageBitmap(bitmap);
}
Thread t = new Thread ( () => UploadToFTP(contentUri.ToString()));
t.Start ();
}
private void UploadToFTP(String uri){
SimpleFTP ftp = new SimpleFTP ();
ftp.Connect("xxx.xxxxx.xxx", 21, "xxxxxxx", "xxxxxxxx");
ftp.Stor (new File(uri));
ftp.Disconnect();
}
Does anyone have an ideia what is happening? I've tried with assets/drawable/files etc. and nothing works. The image can be seen on the ImageView.
The path printed on the console is this one:
file:///storage/sdcard0/Pictures/CameraAppDemo/myPhoto_64ee3fec-08af-4bde-9214-62892b6d9f72.jpg
Thanks
EDIT:
I figured it out... The problem is the use of contentUri.ToString()
instead of contentUri.Path
, this latter the result is the correct and without the "file://" part, so it should be something like that:
/storage/sdcard0/Pictures/CameraAppDemo/myPhoto_64ee3fec-08af-4bde-9214-62892b6d9f72.jpg