Jump to content
KimHJ

How to access a Windows shared folder from Android

Recommended Posts

I'm creating an application for a android device were the user takes a picture and it is store on a local network drive.

After taking the picture I tried this , but it keeps saying that the path doesn't exist.

I was looking around but I haven't fund any thing similar, the Google AI suggested that I use the Android Api contentResolver, but I'm unable locate that unit.

procedure TMainForm.Button2Click(Sender: TObject);
var
PicPath: String;
begin
  PicPath := '\\SERVER\CSPictures';
      try
      if DirectoryExists(PicPath) then
        Image1.Bitmap.SaveToFile(PicPath + '\' + Edit1.Text + '.jpg')
     else
        ShowMessage('Can not find path ' + PicPath);
      except
          on E: Exception do
            ShowMessage('Unable to save picture to' + PicPath);
      end;
end;

Thanks for any help.

Edited by KimHJ

Share this post


Link to post
2 hours ago, KimHJ said:

Google AI suggested that I use the Android Api contentResolver

Is there some reason why you haven't included here what it suggested?

2 hours ago, KimHJ said:

but I'm unable locate that unit.

You can access the ContentResolver via: TAndroidHelper.Context.getContentResolver, by including the Androidapi.Helpers unit.

 

Regardless, I expect you'll need to use a library such as smbj to access files via Windows shares, which means either finding a Delphi implementation or creating imports and writing the code yourself that uses smbj (or some other library that does the same thing).

Share this post


Link to post

If I have an Apache running on the computer could I use TNethttpClient?

 

I have another Delphi/Android app that retrieves bitmaps from a Windows Server in the cloud using TNethttpClient, I know how to do that part.

I will have to find out how to create a conf file to go to a specific path.

Share this post


Link to post

I got it to work with Apache. I had to add 

android:usesCleartextTraffic="true"

in the manifest to be able to save and load with http, since the local machine don't have a SSL.

Share this post


Link to post

I was to quick in saying that I had it.

Loading an image a display it is working.

I'm doing something wrong when I save, if I add a name to the URL like url := 'http://192.168.1.150/test1234.png' I get an error that path is not found.

If I create a png file in the folder with that name I get no errors, but the image is not updated.

I assume I have to name the Image first and the save it, but I don't see any option in TImage to assign a name to the bitmap, the image comes from the camera. Here is my code.

 

procedure TMainForm.SaveImageToURL(const S: String);
var
    ms       : TMemoryStream;
    httpCli  : TNetHTTPClient;
    resp     : IHTTPResponse;
    url      : String;
    Image: TBitmap;
begin

    httpCli := TNetHTTPClient.Create(nil);
    try
        httpCli.UserAgent := 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0';
        ms := TMemoryStream.Create();
        try
            url  := 'http://192.168.1.151/Test1234.png';
            Image  := TBitmap.Create;
                try
                  ms.Seek(0,0);
                  Image := Image1.Bitmap;
                  Image.SaveToStream(ms);
                finally
                  Image.Free;
                end;
            resp := httpCli.Post(url, ms);
            if resp.StatusCode <> 200 then
               Showmessage(Format('HTTP Error=%d %s', [resp.StatusCode, resp.StatusText]))

        finally
            ms.Free;
        end;
    finally
        httpCli.Free;
    end;
end;

 

Share this post


Link to post

I installed Apache and then in the conf/ httpd.conf then i sat the DocumentRoot "C:/myfolder" and <Directory "C:/myfolder" > , AllowOverride All. I can see the Directory from another computer using a web browser and I can open the existing files.

My problem is I don't know if I'm using the right way to save the image, I have been searching, all the example is see is mostly programmers that want add other thing to the files or they are using Indy.

Edited by KimHJ

Share this post


Link to post

Download works out of the box, upload not.

 

You must have a PHP Script, a CGI, a ISAPI dll or an own server to be able to upload files.

Share this post


Link to post

Ok, I will have to install PHP on the local computer where i have Apache running, then add it in the LoadModule.

I found this script in w3schools.com that i will place in the folder, I change the max size from 50000 to 2000000.

<?php
$target_dir = "myfolder/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
  $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
  if($check !== false) {
    echo "File is an image - " . $check["mime"] . ".";
    $uploadOk = 1;
  } else {
    echo "File is not an image.";
    $uploadOk = 0;
  }
}

// Check if file already exists
if (file_exists($target_file)) {
  echo "Sorry, file already exists.";
  $uploadOk = 0;
}

// Check file size
if ($_FILES["fileToUpload"]["size"] > 2000000) {
  echo "Sorry, your file is too large.";
  $uploadOk = 0;
}

// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
  echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
  $uploadOk = 0;
}

// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
  echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
  if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
    echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded.";
  } else {
    echo "Sorry, there was an error uploading your file.";
  }
}
?>

Now my question how to execute the php script from the Delphi Android app, again when I search I only see Indy examples, I don't know if Indy works on Android.

 

Thanks.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×