Skip to content Skip to sidebar Skip to footer

Copying File From A Samba Drive To An Android Sdcard Directory

I am new to Android and Samba. I am trying to use the JCIFS copy. To method to copy a file from a Samba directory to the 'Download' directory under sdcard on an Android 3.1 device.

Solution 1:

The SmbFile's copyTo() method lets you copy files from network to network. To copy files between your local device and the network you need to use streams. E.g.:

try {
    SmbFile source = 
            new SmbFile("smb://username:password@a.b.c.d/sandbox/sambatosdcard.txt");

    File destination = 
            new File(Environment.DIRECTORY_DOWNLOADS, "SambaCopy.txt");

    InputStream in = source.getInputStream();
    OutputStream out = new FileOutputStream(destination);

    // Copy the bits from Instream to Outstream
    byte[] buf = new byte[1024];
    int len;
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }
    // Maybe in.close();
    out.close();

} catch (MalformedURLException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

Post a Comment for "Copying File From A Samba Drive To An Android Sdcard Directory"