JCIFSを使ってwindowsPCと接続したときの処理について

1,506 views
Skip to first unread message

あいくアイス

unread,
Jan 23, 2014, 2:50:52 AM1/23/14
to android-g...@googlegroups.com
先日の質問ではお世話になりました。
おかげさまでandroid端末からwindowsへ繋ぐ事へは成功しました。

しかしいまだ繋がっていることを確認できているだけでディレクトリの表示などができません。
ルートディレクトリからCドライブに入ったり、そこからフォルダを開いたりという処理をしたいのですが色々試してみてもよくわかりませんでした。
SDカードの中のファイルリストの取得などやってみたのですが、Smbファイルで取得した場合だとディレクトリのパスがどうなるか分からなくて困っています。

すみません、本当に申し訳ないのですが助言をお願いします。

Shin Miyazaki

unread,
Jan 23, 2014, 3:58:55 AM1/23/14
to android-g...@googlegroups.com
こんにちは

まずWindowsマシン同士で、共有というものはどういうものなのか学んだ方がいいのでは。


2014年1月23日 16:50 あいくアイス <adkc...@gmail.com>:

--
このメールは Google グループのグループ「日本Androidの会」の登録者に送られています。
このグループから退会し、メールの受信を停止するには、android-group-j...@googlegroups.com にメールを送信します。
このグループに投稿するには、android-g...@googlegroups.com にメールを送信してください。
http://groups.google.com/group/android-group-japan からこのグループにアクセスしてください。
その他のオプションについては、https://groups.google.com/groups/opt_out にアクセスしてください。

Idlks Gtprkht

unread,
Jan 23, 2014, 5:10:40 AM1/23/14
to android-g...@googlegroups.com
コンピュータ名の後に続けて共有名以下を記載すれば繋がるはずです(バックスラッシュ(¥)はスラッシュに変換する必要がありますが)。
前の方のおっしゃる通りPCでのファイル共有の経験が無いのでしたら、いちどSDカードに共有設定してPC同士で繋げてみることをおすすめします。
コンビニのPC入門にも書いてあるような簡単な事です。
よろしくお願いします。

あいくアイス

unread,
Jan 24, 2014, 2:26:06 AM1/24/14
to android-g...@googlegroups.com
 shinmさま Idlks Gtprkhtさま回答ありがとうございます。
windows同士の共有はやったことがあります。

すみません、質問の書き方が悪かったです。
if(v == btn){
            try {
                SmbFile file = new SmbFile("smb://ユーザ名;nil:パスワード@
サーバ名/パス");
     
                String[] filenames = file.list();
                for (int i = 0; i < filenames.length; i++) {
                    System.out.println(filenames[i]);
                }
            } catch (Exception e ) {
                e.printStackTrace();
            }

--------------------------------------------------------------------------------- ↑ ここまでで取得したファイルリストを。

       String path = Environment.getExternalStorageDirectory().getAbsolutePath();

                File dir = new File(path);
                final File[] files = dir.listFiles();
                final String[] items;
                items = new String[files.length + 1];
                for (int i = 0; i < files.length; i++) {
                    File file = files[i];
                    items[i] = file.getName();
                }

こちらは某サイトから引用しました
--------------------------------------------------------------------------------- ↑ ここで反映させたい

本当は一番上は

String path = Environment.getExternalStorageDirectory().getAbsolutePath();

File dir = new File(path);

この様になっていて
Environment.getExternalStorageDirectory().getAbsolutePath();
の部分はSDカードのパスを取得する記述になっています。

このあと取得したファイルリストをAlertDialogで表示させるという手順になっているんですが
SDカードの中ではなくSmbファイルで取得したファイルリストを表示させたいと思ってます、がパスの記述の仕方がよく分かりません。
よろしくお願いします。

Shin Miyazaki

unread,
Jan 24, 2014, 5:33:27 AM1/24/14
to android-g...@googlegroups.com
こんにちは

パスとは?
ファイル一覧ならそのSDカードの例と同じようにすればいいだけですが、何に引っかかっているのでしょう?
以下のいずれか(あるいはその他)のお好きな名前を出せばいいだけでは?
SmbFile [] files = file.listFiles();
for (SmbFile f : files) {
    String name = f.getName(); // ファイル(ディレクトリ)名のみ
    String uncPath = f.getUncPath(); // UNCパス。Widnowsでシェア指定するときの形式
    String smbPath = f.getPath(); // SMB URL。※※アカウント情報含んだSMB URLを指定した場合、それらも含まれるので表示には注意※※
}


2014/1/24 あいくアイス <adkc...@gmail.com>

--

Idlks Gtprkht

unread,
Jan 24, 2014, 5:56:40 AM1/24/14
to android-g...@googlegroups.com
すいません、ちょっときつい言い方になります。

printlnでPCのファイルの内容は表示されているのですか? これは状況を判断する上で非常に重要な事だと普通思いますが、何故書いていないのでしょう?
表示されているなら、もうJCIFSの問題ではないと思います、JAVAの基本的な作り方の話ではないでしょうか?
出ているのなら、出てきた内容をprintlnじゃなく変数なりに溜め込むとかして、AlertDialogのリストビューに出せばいい話です。
AlertDialogでおちているならLogCatを見て分からなければ、添付すべきなのに何故していないのでしょう?
なんにせよ、質問の内容や情報の提供の仕方が雑・適当な印象を受けます(私も人の事いえませんが)。
初心者お断り的な雰囲気は出したくないのですが、今の質問の仕方やソースの提供の仕方を見ると永遠に問題は解決されないような気がします。

色々と失礼な事を申し訳ありません。

よろしくお願いします。
Message has been deleted

あいくアイス

unread,
Jan 31, 2014, 8:36:14 PM1/31/14
to android-g...@googlegroups.com
返信が遅くなって本当に申し訳ありません。
そうですね、質問の仕方がすごく雑だったと思ました。
ファイルリストの取得は
try {
        SmbFile file = new SmbFile("smb://Administrator:Andr...@10.32.2.1/utukemono/
");
   
        String[] filenames = file.list();
        for (int i = 0; i < filenames.length; i++) {
           System.out.println(filenames[i]);
           Log.d("test",filenames[i]);

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

このようになっているのですが

System.out.println(filenames[i]);
           Log.d("test",filenames[i]);

↑ここのところでLogcatにファイルの内容は表示しています。
あとはIdlks Gtprkhtさまの言うとおりprintlnではなく変数に溜め込んでAlertDialogにて表示させればいいのですがそこで詰まっています。
JCIFSを使ってのファイルリストの取得はAsyncTaskを使って非同期処理でやっていてソースコードはこんな感じです。

バックグラウンド処理--------------------------------------------------------------------

package com.example.asynctest;

import jcifs.smb.SmbFile;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.TextView;

public class AsyncTest2
  extends AsyncTask<String, Integer, Long>



  implements OnCancelListener{

  final String TAG = "MyAsyncTask";
  ProgressDialog dialog;
  Context context;
 
  public AsyncTest2(Context context){
    this.context = context;
   }
 
 

  @Override
  protected void onPreExecute() {
    Log.d(TAG, "onPreExecute");
 
  }
 
  @Override
 
  protected Long doInBackground(String... params) {
    Log.d(TAG, "doInBackground - " + params[0]);
  //グローバル変数
  Globals globals;
  //グローバル変数を取得
  globals = (Globals) this.getApplication();
    try {
        SmbFile file = new SmbFile("smb://ユーザ名:パスワード@コンピュータ名/");
         globals.files = file.listFiles();
      
        globals.items = new String[globals.files.length + 1];
        
        for (SmbFile f : globals.files) {
            int i = 0;

            String name = f.getName(); // ファイル(ディレクトリ)名のみ
            String uncPath = f.getUncPath(); // UNCパス。Widnowsでシェア指定するときの形式
            String smbPath = f.getPath(); // SMB URL。※※アカウント情報含んだSMB URLを指定した場合、それらも含まれるので表示には注意※※
            System.out.println(name);
            System.out.println(uncPath);
            System.out.println(smbPath);
            globals.items[i] = name;
            i++;
        }
        globals.items[globals.files.length] = "キャンセル";

     
       
    } catch (Exception e ) {
        e.printStackTrace();
    }
   
    return 123L;
  }

private Globals getApplication() {
    // TODO 自動生成されたメソッド・スタブ
    return null;
}


@Override
  protected void onProgressUpdate(Integer... values) {
    Log.d(TAG, "onProgressUpdate - " + values[0]);
    //dialog.setProgress(values[0]);
 
}
  @Override
  protected void onCancelled() {
    Log.d(TAG, "onCancelled");
    //dialog.dismiss();
  }

  @Override
  protected void onPostExecute(Long result) {
    Log.d(TAG, "onPostExecute - " + result);
   
  
  }

  private TextView findViewById(Object textView1) {
    // TODO 自動生成されたメソッド・スタブ
    return null;
}



@Override
  public void onCancel(DialogInterface dialog) {
    Log.d(TAG, "Dialog onCancell... calling cancel(true)");
    this.cancel(true);
  }
}

メインスレッド--------------------------------------------------------------------

package com.example.asynctest;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class AsyncTest extends Activity
  implements OnClickListener {
  private Button btn0,btn1,btn2;
//グローバル変数
  Globals globals;
  final String TAG = "AsyncTest";
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_async_test);
    ((Button)findViewById(R.id.button1))
      .setOnClickListener(this);
    btn0 = (Button)findViewById(R.id.button2);
    btn0.setOnClickListener(this);   
   
    btn1 = (Button)findViewById(R.id.button3);
    btn1.setOnClickListener(this);
   
    btn2 = (Button)findViewById(R.id.button4);
    btn2.setOnClickListener(this);
  //グローバル変数を取得
    globals = (Globals) this.getApplication();
   

    }
   
 
  @Override
  public void onClick(View v) {
    switch(v.getId()){
        case R.id.button1:
            new AsyncTest2(this).execute("Param1");
        break;
       
        case R.id.button2:
            btn0.setText("FileActivity");
            Intent intent = new Intent(this, FileActivity.class);
            startActivityForResult(intent, 0);
        break;
       
        case R.id.button3:
            btn1.setText("SubThred");
            Intent intent1 = new Intent(this, SubThred.class);
            startActivityForResult(intent1,0);
        break;
       
        case R.id.button4:
            btn2.setText("表示");
            /*
            EditText editText = (EditText)findViewById(R.id.edittext0);
            globals = (Globals) this.getApplication();
            Editable input=editText.getText();
            editText.settext(globals.items);
            */
            new AlertDialog.Builder(this)
            .setTitle("Select an item !")
             .setItems(globals.items, new DialogInterface.OnClickListener(){
               @Override
                public void onClick(DialogInterface dialog, int i) {
                  Toast.makeText(AsyncTest.this
                          , String.format("%s が選択されました。", globals.items[i])
                             , Toast.LENGTH_LONG).show();
              }
             })
            .show();
    }
  }
 
}




これでは配列の”items”をグローバル変数にしてバックグラウンドでファイルリストを”items”に入れた後にメインスレッドでAlertDialogで呼び出しています。
しかしJCIFSでファイルリストを取得するところですでにエラーが出ています。
グローバル変数を使う前はエラーが出ていなかったのと、Logcatを見てみるとグローバル変数を使っているところでsystem.errと出ているのでおそらくグローバル変数の使い方が間違っているのだと思いますがどこが間違っているのか分かりません。

Logcat--------------------------------------------------------------------------------------------------------

java.lang.NullPointerException
    at com.example.asynctest.AsyncTest2.doInBackground(AsyncTest2.java:45)
    at com.example.asynctest.AsyncTest2.doInBackground(AsyncTest2.java:1)
    at android.os.AsyncTask$2.call(AsyncTask.java:252)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1081)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:574)
    at java.lang.Thread.run(Thread.java:1020)

グローバル変数を定義しているクラス----------------------------------------------------------------
package com.example.asynctest;

import jcifs.smb.SmbFile;
import android.app.Application;


public class Globals extends Application {
    String[] items;
    SmbFile[] files;
    public void GlobalsAllInit() {
   
     }
}

ManifestFile-----------------------------------------------------------------------------------------------

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.asynctest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

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

    <library />

    <application
        android:name="com.example.asynctest.Globals"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        >
        <activity
            android:name="com.example.asynctest.AsyncTest"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.asynctest.FileActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.asynctest.Globals"
            android:label="@string/title_activity_globals" >
        </activity>
        <activity
            android:name="com.example.asynctest.SubThred"
            android:label="@string/title_activity_sub_thred" >
        </activity>
    </application>

</manifest>

よろしくお願いします。


2014年1月24日金曜日 19時56分40秒 UTC+9 Idlks Gtprkht:

Shinichi Arata

unread,
Feb 1, 2014, 4:01:26 AM2/1/14
to android-g...@googlegroups.com

かっぱ大王です。

ログ出力通りのエラーだと思います。

java.lang.NullPointerException
    at com.example.asynctest.AsyncTest2.doInBackground(AsyncTest2.java:45)
    at com.example.asynctest.AsyncTest2.doInBackground(AsyncTest2.java:1)
    at android.os.AsyncTask$2.call(AsyncTask.java:252)

とあるように、nullのオブジェクトを参照しようとしてエラーになっています。

Globalsクラスのオブジェクトの取得を以下のようにされています。

  globals = (Globals) this.getApplication();
 
        globals.files = file.listFiles();

private Globals getApplication() {
    // TODO 自動生成されたメソッド・スタブ
    return null;
}

この時、this.getApplication()に対して、nullが返されるため、
globals.filesのアクセス時にNullPointerExceptionが出ています。

getApplicationメソッドで適切にインスタンスを生成する必要があります。

よろしくお願いします。


2014年2月1日 10:36 あいくアイス <adkc...@gmail.com>:

--

あいくアイス

unread,
Feb 6, 2014, 3:36:04 AM2/6/14
to android-g...@googlegroups.com
たびたび返答が遅くなり申し訳ありません!
そして、かっぱ大王様返答ありがとうございます!
なるほど、確かにログのとおりですね・・・早速やってみます。

2014年2月1日土曜日 18時01分26秒 UTC+9 かっぱ大王:

かっぱ大王です。

ログ出力通りのエラーだと思います。

java.lang.NullPointerException
    at com.example.asynctest.AsyncTest2.doInBackground(AsyncTest2.java:45)
    at com.example.asynctest.AsyncTest2.doInBackground(AsyncTest2.java:1)
    at android.os.AsyncTask$2.call(AsyncTask.java:252)

とあるように、nullのオブジェクトを参照しようとしてエラーになっています。

Globalsクラスのオブジェクトの取得を以下のようにされています。

  globals = (Globals) this.getApplication();
 
        globals.files = file.listFiles();

private Globals getApplication() {
    // TODO 自動生成されたメソッド・スタブ
    return null;
}

この時、this.getApplication()に対して、nullが返されるため、
globals.filesのアクセス時にNullPointerExceptionが出ています。

getApplicationメソッドで適切にインスタンスを生成する必要があります。

よろしくお願いします。
2014年2月1日 10:36 あいくアイス <adkc...@gmail.com>:
返信が遅くなって本当に申し訳ありません。
そうですね、質問の仕方がすごく雑だったと思ました。
ファイルリストの取得は
try {
        SmbFile file = new SmbFile("smb://Administrator:Andro...@10.32.2.1/utukemono/
このグループから退会し、メールの受信を停止するには、android-group-japan+unsubscribe@googlegroups.com にメールを送信します。
このグループに投稿するには、android-group-ja...@googlegroups.com にメールを送信してください。

ohisamallc

unread,
Feb 11, 2014, 7:26:32 PM2/11/14
to android-g...@googlegroups.com
山形のohisamaです。
山形は、雪が少ないです。
がんばろう、東北。
私事ですが、週末、「Hack for Town in Aizu」に参加します。
見かけたら、声、掛けてください。
jsdo.it」に、オープンデータを読んで、マップにマークするソース上げてます。
良かったら見てください。

掲題の件
フォルダ(デレクトリ)の場合は、最後に「/」(スラッシュ)が必要です。
地雷ですね。
SmbFile file = new SmbFile("smb://Administrator:Andr...@10.32.2.1/utukemono/
このグループから退会し、メールの受信を停止するには、android-group-j...@googlegroups.com にメールを送信します。
このグループに投稿するには、android-g...@googlegroups.com にメールを送信してください。
http://groups.google.com/group/android-group-japan からこのグループにアクセスしてください。
その他のオプションについては、https://groups.google.com/groups/opt_out にアクセスしてください。



--
このメールは Google グループのグループ「日本Androidの会」の登録者に送られています。
このグループから退会し、メールの受信を停止するには、android-group-j...@googlegroups.com にメールを送信します。
このグループに投稿するには、android-g...@googlegroups.com にメールを送信してください。
Reply all
Reply to author
Forward
0 new messages