Android从pc文件夹中获取文件

我的
Android设备与本地PC之间的连接有问题.我想从我的电脑中共享的文件夹中获取所有文件.

这是我的代码:

private void connectToPC(){
        try {
            String username = "Alex";
            String password = ".";
            String sharedFolder = "Fisiere Date Senzori";
            String path = "smb://"+ip+"/Users/Alex/Documents/"+sharedFolder+"/";

            NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("",username,password);
            SmbFile smbFile = new SmbFile(path,auth);

            String[] files = smbFile.list();

            for (String fileName : files) {
                Log.d("log_tag", "Fisier: " + fileName);
            }

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

这是我得到的结果:

06-29 22:35:37.752: W/System.err(17081): jcifs.smb.SmbException: Failed to connect to server
06-29 22:35:37.752: W/System.err(17081): java.net.UnknownHostException: null
06-29 22:35:37.752: W/System.err(17081):    at jcifs.UniAddress.getAllByName(UniAddress.java:315)
06-29 22:35:37.752: W/System.err(17081):    at jcifs.smb.SmbFile.getFirstAddress(SmbFile.java:810)
06-29 22:35:37.752: W/System.err(17081):    at jcifs.smb.SmbFile.connect(SmbFile.java:897)
06-29 22:35:37.752: W/System.err(17081):    at jcifs.smb.SmbFile.connect0(SmbFile.java:826)
06-29 22:35:37.752: W/System.err(17081):    at jcifs.smb.SmbFile.resolveDfs(SmbFile.java:664)
06-29 22:35:37.752: W/System.err(17081):    at jcifs.smb.SmbFile.send(SmbFile.java:726)
06-29 22:35:37.752: W/System.err(17081):    at jcifs.smb.SmbFile.doFindFirstNext(SmbFile.java:1930)
06-29 22:35:37.752: W/System.err(17081):    at jcifs.smb.SmbFile.doEnum(SmbFile.java:1682)
06-29 22:35:37.752: W/System.err(17081):    at jcifs.smb.SmbFile.list(SmbFile.java:1653)
06-29 22:35:37.752: W/System.err(17081):    at jcifs.smb.SmbFile.list(SmbFile.java:1550)
06-29 22:35:37.752: W/System.err(17081):    at com.rasp.pi.StartingPoint.connectToPC(StartingPoint.java:149)
06-29 22:35:37.752: W/System.err(17081):    at com.rasp.pi.StartingPoint.onCreate(StartingPoint.java:56)
06-29 22:35:37.752: W/System.err(17081):    at android.app.Activity.performCreate(Activity.java:6289)
06-29 22:35:37.752: W/System.err(17081):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
06-29 22:35:37.752: W/System.err(17081):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2647)
06-29 22:35:37.752: W/System.err(17081):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2759)
06-29 22:35:37.752: W/System.err(17081):    at android.app.ActivityThread.access$900(ActivityThread.java:178)
06-29 22:35:37.752: W/System.err(17081):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1449)
06-29 22:35:37.752: W/System.err(17081):    at android.os.Handler.dispatchMessage(Handler.java:102)
06-29 22:35:37.752: W/System.err(17081):    at android.os.Looper.loop(Looper.java:145)
06-29 22:35:37.752: W/System.err(17081):    at android.app.ActivityThread.main(ActivityThread.java:5944)
06-29 22:35:37.752: W/System.err(17081):    at java.lang.reflect.Method.invoke(Native Method)
06-29 22:35:37.752: W/System.err(17081):    at java.lang.reflect.Method.invoke(Method.java:372)
06-29 22:35:37.752: W/System.err(17081):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
06-29 22:35:37.752: W/System.err(17081):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
06-29 22:35:37.752: W/System.err(17081):    at jcifs.smb.SmbFile.connect0(SmbFile.java:828)
06-29 22:35:37.752: W/System.err(17081):    at jcifs.smb.SmbFile.resolveDfs(SmbFile.java:664)
06-29 22:35:37.752: W/System.err(17081):    at jcifs.smb.SmbFile.send(SmbFile.java:726)
06-29 22:35:37.752: W/System.err(17081):    at jcifs.smb.SmbFile.doFindFirstNext(SmbFile.java:1930)
06-29 22:35:37.752: W/System.err(17081):    at jcifs.smb.SmbFile.doEnum(SmbFile.java:1682)
06-29 22:35:37.762: W/System.err(17081):    at jcifs.smb.SmbFile.list(SmbFile.java:1653)
06-29 22:35:37.762: W/System.err(17081):    at jcifs.smb.SmbFile.list(SmbFile.java:1550)
06-29 22:35:37.762: W/System.err(17081):    at com.rasp.pi.StartingPoint.connectToPC(StartingPoint.java:149)
06-29 22:35:37.762: W/System.err(17081):    at com.rasp.pi.StartingPoint.onCreate(StartingPoint.java:56)
06-29 22:35:37.762: W/System.err(17081):    at android.app.Activity.performCreate(Activity.java:6289)
06-29 22:35:37.762: W/System.err(17081):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
06-29 22:35:37.762: W/System.err(17081):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2647)
06-29 22:35:37.772: W/System.err(17081):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2759)
06-29 22:35:37.772: W/System.err(17081):    at android.app.ActivityThread.access$900(ActivityThread.java:178)
06-29 22:35:37.772: W/System.err(17081):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1449)
06-29 22:35:37.772: W/System.err(17081):    at android.os.Handler.dispatchMessage(Handler.java:102)
06-29 22:35:37.772: W/System.err(17081):    at android.os.Looper.loop(Looper.java:145)
06-29 22:35:37.772: W/System.err(17081):    at android.app.ActivityThread.main(ActivityThread.java:5944)
06-29 22:35:37.772: W/System.err(17081):    at java.lang.reflect.Method.invoke(Native Method)
06-29 22:35:37.772: W/System.err(17081):    at java.lang.reflect.Method.invoke(Method.java:372)
06-29 22:35:37.772: W/System.err(17081):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
06-29 22:35:37.772: W/System.err(17081):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

此方法在Android Activity的onCreate方法中调用.我使用的是jcifs-1.3.8.jar.我该怎么做才能使这个工作?

谢谢.

最佳答案 我测试了你的代码并且它工作得很好(参数不同),似乎ip变量是全局的,在调用connectToPC()之前仔细检查它的值是否正确.

Edit

不要忘记许可

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

Edit

这是connectToPC与我的修改方法.它对我来说很好.

private void connectToPC(){
    try {
        String ip = "192.168.1.61";
        String username = "omainegra";
        String password = "123";
        String sharedFolder = "D/";

        String path = "smb://" + ip + "/" + sharedFolder;
        NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("",username,password);

        SmbFile smbFile = new SmbFile(path,auth);

        String[] files = smbFile.list();

        for (String fileName : files) {
            Log.e("log_tag", "Fisier: " + fileName);
        }

    } catch (IOException e) {
        e.printStackTrace();
        Log.e("", "", e);
    }
}
点赞