2017-05-08 18 views
2

AndroidでProgramABを使用しています。そこに/ bots /ディレクトリを作成してaimlファイルをアセットフォルダから外部ストレージにコピーする必要があります。これは私がthisから得たコードです。このチュートリアルでは、アンドロイドでチャットボットについて教えています(このコードで呼び出される他の関数はディレクトリのコピーとは関係ありません)。アセットフォルダのサブディレクトリを外部ストレージ内の特定のフォルダにコピー

私は他のソリューションを試しましたが、何も私のために働いていませんでした。

//checking SD card availablility 
     boolean a = isSDCARDAvailable(); 
     //receiving the assets from the app directory 
     AssetManager assets = getResources().getAssets(); 
     File jayDir = new File(Environment.getExternalStorageDirectory().toString() + "/hari/bots/Hari"); 
     boolean b = jayDir.mkdirs(); 
     if (jayDir.exists()) { 
      //Reading the file 
      try { 
       for (String dir : assets.list("Hari")) { 
        File subdir = new File(jayDir.getPath() + "/" + dir); 
        boolean subdir_check = subdir.mkdirs(); 
        for (String file : assets.list("Hari/" + dir)) { 
         File f = new File(jayDir.getPath() + "/" + dir + "/" + file); 
         if (f.exists()) { 
          continue; 
         } 
         InputStream in = null; 
         OutputStream out = null; 
         in = assets.open("Hari/" + dir + "/" + file); 
         out = new FileOutputStream(jayDir.getPath() + "/" + dir + "/" + file); 
         //copy file from assets to the mobile's SD card or any secondary memory 
         copyFile(in, out); 
         in.close(); 
         in = null; 
         out.flush(); 
         out.close(); 
         out = null; 
        } 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     //get the working directory 
     MagicStrings.root_path = Environment.getExternalStorageDirectory().toString() + "/hari"; 
     System.out.println("Working Directory = " + MagicStrings.root_path); 
     AIMLProcessor.extension = new PCAIMLProcessorExtension(); 
     //Assign the AIML files to bot for processing 
     bot = new Bot("Hari", MagicStrings.root_path, "chat"); 
     chat = new Chat(bot); 
     String[] args = null; 
     mainFunction(args); 
+0

'boolean b = jayDir.mkdirs();'。ディレクトリがまだ存在しない場合にのみ、mkdirsを呼び出す必要があります。 mkdirsがfalseを返す場合は、そのことを伝えるユーザーにトーストを表示する必要があります。そして戻る。今あなたは盲目的に続行します。コードを守ってください。 – greenapps

+0

@greenapps私は本当にアンドロイドに新しいですし、私はコピーが実際に起こっているかどうかを確認することさえできません。あなたの記載された問題のためにコードがタスクを実行していませんか? –

+0

あなたは何がうまくいくのか、何がうまくいかないのかを教えてください。ログステートメントとトーストを使用して何が起こるかを確認します。いくつかの基本的なデバッグを行います。 – greenapps

答えて

2

このコードはすごくうまく機能しています。 ここに全体がtutorialです。

/** 
* Copy the asset at the specified path to this app's data directory. If the 
* asset is a directory, its contents are also copied. 
* 
* @param path 
* Path to asset, relative to app's assets directory. 
*/ 
private void copyAsset(String path) { 
    AssetManager manager = getAssets(); 

    // If we have a directory, we make it and recurse. If a file, we copy its 
    // contents. 
    try { 
    String[] contents = manager.list(path); 

    // The documentation suggests that list throws an IOException, but doesn't 
    // say under what conditions. It'd be nice if it did so when the path was 
    // to a file. That doesn't appear to be the case. If the returned array is 
    // null or has 0 length, we assume the path is to a file. This means empty 
    // directories will get turned into files. 
    if (contents == null || contents.length == 0) 
     throw new IOException(); 

    // Make the directory. 
    File dir = new File(getExternalFilesDir(null), path); 
    dir.mkdirs(); 

    // Recurse on the contents. 
    for (String entry : contents) { 
     copyAsset(path + "/" + entry); 
    } 
    } catch (IOException e) { 
    copyFileAsset(path); 
    } 
} 

/** 
* Copy the asset file specified by path to app's data directory. Assumes 
* parent directories have already been created. 
* 
* @param path 
* Path to asset, relative to app's assets directory. 
*/ 
private void copyFileAsset(String path) { 
    File file = new File(getExternalFilesDir(null), path); 
    try { 
    InputStream in = getAssets().open(path); 
    OutputStream out = new FileOutputStream(file); 
    byte[] buffer = new byte[1024]; 
    int read = in.read(buffer); 
    while (read != -1) { 
     out.write(buffer, 0, read); 
     read = in.read(buffer); 
    } 
    out.close(); 
    in.close(); 
    } catch (IOException e) { 
    Log.e(e); 
    } 
} 
+0

これは完全に動作しています!私は実際には同じブログに従っていた:) –

関連する問題