4

I need to connect from my Android phone to a Windows PC share and access files. I saw some sample apps in Android market that access share folders using smb/samba. But I have no idea about how to create an app like that.

Syscall
  • 19,327
  • 10
  • 37
  • 52
rwe
  • 177
  • 1
  • 1
  • 11

2 Answers2

6

You need to get JCIFS and use SmbFile class to interact with files over the network,

http://lists.samba.org/archive/jcifs/2007-September/007465.html

that is a quick example of how to list files, of coarse you need internet permission on. So Far though everytime I try to call SmbFile.listFiles(); I get an UnknownHostException, However others seam to be able to do it with no problem, this might work for you, try it!

Simon Featherstone
  • 1,716
  • 23
  • 40
tantonj
  • 434
  • 5
  • 18
  • I join your club tantonj, I always get issues though some times by mistake I'm able to connect with the same code, same network, same router ..etc but 5 minutes later my app throws an error in my face :-/ – JustADev Aug 24 '14 at 09:36
2

Google has released a simple, free Samba client. It is on github so you can have a look and use whatever you need out of that: https://github.com/google/samba-documents-provider

The other option is JCIFS: https://jcifs.samba.org/. There you can find the library and examples on how to use it.

I used JCIFS. Here is an example from my code which reads files from a folder in a windows share:

    TreeMap<Date, String> filesInfo = new TreeMap<Date, String>();
    NtlmPasswordAuthentication auth = null;
        UniAddress dc = UniAddress.getByName(m_dataHostIp);
        if(m_userName.length() > 0 && m_password.length() > 0)
            auth = new NtlmPasswordAuthentication(m_domain + ";" + m_userName + ":" + m_password);
        else
            auth = new NtlmPasswordAuthentication(m_domain, null, null);
        SmbSession.logon(dc, auth);

        SmbFile file = new SmbFile(m_foldername, auth);
        SmbFile[] files = file.listFiles();
        for (int i = 0; i < files.length; i++)
        {
           String fileName = files[i].getName();
           String extension=fileName.substring(fileName.lastIndexOf(".") + 1);
           logInfo(TAG + " " + fileName + "\n");
           Date fileTime = new Date(files[i].getDate()); 
            if(m_fileExtension.contains(extension))
                filesInfo.put(fileTime, fileName);

        }

The code posted above works. It allows you to connect to the share, authenticate (username and password that you know) and get the list of the files. At the root of jcif file access is the SmbFile which has all the info you need to access files in the share. All you need is in your build.gradle for the app add:

dependencies {
    implementation files('libs/jcifs-1.3.19.jar')
}

and in your implementation file:

import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbFile;
import static jcifs.smb.SmbFile.FILE_SHARE_DELETE;
import static jcifs.smb.SmbFile.FILE_SHARE_READ;
import static jcifs.smb.SmbFile.FILE_SHARE_WRITE;
Gogu CelMare
  • 699
  • 6
  • 15