Android从pc文件夹中获取文件

时间:2015-06-29 20:01:19

标签: android

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

这是我的代码:

MATCH (n:Topic { mongoId: _id })-[r]-() RETURN COLLECT(r.mongoId);

这是我得到的结果:

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

此方法在Android活动的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) 方法中调用。我正在使用onCreate。我该怎么做才能使这个工作?

感谢。

1 个答案:

答案 0 :(得分:2)

我测试了你的代码,它运行得很好(参数不同),似乎ip变量是全局的,在调用connectToPC()之前仔细检查它的值是否正确。

  

修改

不要忘记许可

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

修改

这是我修改的方法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);
    }
}