Copying file from a Samba drive to an Android sdcard directory

﹥>﹥吖頭↗ 提交于 2019-12-11 11:44:19

问题


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. Following is my code:

from = new SmbFile("smb://username:password@a.b.c.d/sandbox/sambatosdcard.txt");
File root = Environment.getExternalStorageDirectory();
File sourceFile = new File(root + "/Download", "SambaCopy.txt");
to = new SmbFile(sourceFile.getAbsolutePath());
from.copyTo(to);

I am getting a MalformedURLException on the 'to' file. Is there a way to get around this problem using the copyTo method, or is there an alternate way to copy a file from the samba folder to the sdcard folder using JCIFS or any other way? Thanks.


回答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();
}


来源:https://stackoverflow.com/questions/8169593/copying-file-from-a-samba-drive-to-an-android-sdcard-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!