无法将文件从SMB复制到手机内存

时间:2017-11-21 04:45:59

标签: android file copy smb

Hello其他程序员,我很难将文件从Windows共享文件夹复制到手机内存。我调查了好几个小时,但我却被误导了。

java.io.FileNotFoundException:Download / example.pdf:open failed:ENOENT(没有这样的文件或目录)

我需要权限

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

也适用于Android 6.0及以上版本

if (android.os.Build.VERSION.SDK_INT >= 23){

        if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {

            requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
                            Manifest.permission.READ_EXTERNAL_STORAGE},
                    MY_PERMISSIONS_REQUEST);

            return;
        }
        else   {}


    }

这是我的工作

NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(DOMAIN, USERNAME, PASSWORD);
            SmbFile dir = null;


            try {
                //String PATH = "smb://192.168.8.100/share/";

                SmbFile source = new SmbFile(PATH+"example.pdf",auth);
                File destination = new File(Environment.DIRECTORY_DOWNLOADS, "example.pdf");
                InputStream in = source.getInputStream();
                OutputStream out = new FileOutputStream(destination);
                byte[] buf = new byte[1024];
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
                out.close();

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

任何想法我能做错什么?

0 个答案:

没有答案