2016-05-12 6 views
0

誰もがなぜこれが機能しないのか説明できますか?私はフォルダのパスを取得できますが、サブフォルダはありません

私はこのように私のフォルダのパスを取得 -

// getting SDcard root path 
File myDirectory = new File(Environment.getExternalStorageDirectory() 
     + "/SecuDrive/"); 

をして、これは私がinstance-

のための "x"

@Override 
    protected Boolean doInBackground(Void... params) { 
     File listFile[] = myDirectory.listFiles(); 
     if (listFile != null) { 
      for (int i = 0; i < listFile.length; i++) { 
       if (listFile[i].isDirectory()) { 

       } else { 
        String fPath = listFile[i].getPath(); 

        for (String ext : TARGET_EXTENSIONS) { 
         fPath = fPath.replace("." + ext, 
           "x" + ext); 
        } 

        listFile[i].renameTo(new File(fPath)); 
       } 
      } 
     } 
     return true; 
    } 

ですべてのmp3(音声)ファイル名にドットを置き換える方法です

ninja.mp3 - > ninjaxmp3

しかし、これはサブディレクトリ内のファイルの名前を変更していません。親ディレクトリ(/ SecuDrive /)内のファイルの名前を変更するだけです。

+0

あなたが何もしないしているとして、それはそうでしょうか '場合(LISTFILE [Iを試してみてください] .isDirectory()){'サブディレクトリの状態.. – ELITE

+0

hmmm私はその行の後に置くべきですか? – abbie

+0

投票の上に以下の答えを参照してください。 – ELITE

答えて

0
  1. コードのパフォーマンスが効率的ではありません。私のコードを確認してください

2.Reasonあなたがサブディレクトリのファイルを見つけることはありません。あなたのコードでは、ディレクトリを見つけたら、そのディレクトリを再帰的に検索する必要があります。

あなたのプロジェクトにこのコードをコピーし、doInBackgroundメソッドでgetFile()を呼び出すことをお勧めします。

UPDATED ANSWER:

private class loaderexample extends AsyncTask<Void,Void,Void>{ 
File myDirectory; 
    private Context ctx; 
    private loader(Context ctx){ 
     this.ctx=ctx; 
    } 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
myDirectory = new File(Environment.getExternalStorageDirectory() 
    + "/SecuDrive/"); 

    } 

    @Override 
    protected Void doInBackground(Void... params) { 

     getFile(myDirectory); 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Void aVoid) { 
     super.onPostExecute(aVoid); 
    } 

    private void getFile(File dir) { 

    File[] listFile = dir.listFiles(new FileFilter() { 
     @Override 
     public boolean accept(File dir) { 
      String name = dir.toString(); 

      if (dir.isDirectory()) { 
       getFile(dir); //calling same method inside 
       return false; 
      } 
      else{ 
       // you get all files here.. try to rename all files here. use variable "name" to do your action like renaming as you said 
      } 
      return false; 
     } 
    }); 

} 




} 
+0

を編集して編集しました – sandesh

+0

私はあなたに何を伝えるべきかわかりませんメソッドのすべての変数はローカル変数です。すべてのファイル拡張が必要な​​場合は、if条件を削除し、else(すべてのファイルを取得します)を使用します。 getFile(myDirectory)を置くだけです。あなたのdoInbcakgroundメソッドで。 – sandesh

+0

10分前にあなたの問題を解決しました。あなたが望むすべてのファイルにコードを変更しました。実際にコードを実行したときと同じように、mycode内のすべてのファイルの名前を変更するだけです。 getFile(myDirectory)を呼び出します。 doInBackgroundメソッド内。それでおしまい。 – sandesh

2

はこの

public void listFile(String pathname) { 
File f = new File(pathname); 
File[] listfiles = f.listFiles(); 
for (int i = 0; i < listfiles.length; i++) { 
    if (listfiles[i].isDirectory()) { 
     File[] internalFile = listfiles[i].listFiles(); 
     for (int j = 0; j < internalFile.length; j++) { 
      System.out.println(internalFile[j]); 
      if (internalFile[j].isDirectory()) { 
       String name = internalFile[j].getAbsolutePath(); 
       listFile(name); 
      } 

     } 
    } else { 
     System.out.println(listfiles[i]); 
    } 

} 

}

+0

私はasynctaskの男を使用したいと私はファイルの名前を変更したい! – abbie

関連する問題