2012-06-18 27 views
6

私は起動時にSDカードの/ mnt/sdcard/Android/data/pathにコピーする必要のあるファイルとディレクトリがほとんどないフォルダがあります初めてのアプリケーションです。もちろん、まだ必要なフォルダがない場合は、そのパスには存在しません。res/rawフォルダからsdカードにディレクトリとファイルをコピーする - android

私のアプリケーションのres/rawフォルダ内にこのフォルダがあります。

フォルダとそのすべてのコンテンツをres/rawからSDカード内の指定されたパスにコピーできるようにするために必要な手順は何ですか?

ご迷惑をおかけして申し訳ありません。私はあなたが資産にあなたのファイルを保持示唆

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    copyFileOrDir("edu1");//directory name in assets 
} 
File sdCard = Environment.getExternalStorageDirectory(); 
private void copyFileOrDir(String path) { 
    AssetManager assetManager = this.getAssets(); 
    String assets[] = null; 
    try { 
     assets = assetManager.list(path); 
     if (assets.length == 0) { 
      copyFile(path); 
     } else { 
      File dir = new File (sdCard.getAbsolutePath() + "/" + "Android/data"); 
      //String fullPath = "/data/data/" + this.getPackageName() + "/" + path;//path for storing internally to data/data 
      //File dir = new File(fullPath); 
      if (!dir.exists()){ 
       System.out.println("Created directory"+sdCard.getAbsolutePath() + "/Android/data"); 
       boolean result = dir.mkdir(); 
       System.out.println("Result of directory creation"+result); 
      } 

      for (int i = 0; i < assets.length; ++i) { 
       copyFileOrDir(path + "/" + assets[i]); 
      } 
     } 
    } catch (IOException ex) { 
     System.out.println("Exception in copyFileOrDir"+ex); 
    } 
} 

private void copyFile(String filename) { 
    AssetManager assetManager = this.getAssets(); 

    InputStream in = null; 
    OutputStream out = null; 
    try { 
     in = assetManager.open(filename); 
     //String newFileName = "/data/data/" + this.getPackageName() + "/" + filename;//path for storing internally to data/data 
     String newFileName = sdCard.getAbsolutePath() + "/Android/data/" + filename; 
     out = new FileOutputStream(newFileName); 
     byte[] buffer = new byte[1024]; 
     int read; 
     while ((read = in.read(buffer)) != -1) { 
      out.write(buffer, 0, read); 
     } 
     in.close(); 
     in = null; 
     out.flush(); 
     out.close(); 
     out = null; 
    } catch (Exception e) { 
     System.out.println("Exception in copyFile"+e); 
    } 

} 
} 
+0

これまでに行ったことについていくつかのコードを投稿してもよろしいですか?あなたがちょうど「私がやりたいことがここにあります」と言っているようです。 –

+0

本当に、私は自分の質問を編集しています。 – user1400538

答えて

2

:それは他の誰か場合に役立ちます

編集

次は、ソリューションです。次のコードは、コンテンツディレクトリからSDカードにコンテンツをコピーするのに役立ちます。

public static void copyFile(Activity c, String filename) 
{ 
    AssetManager assetManager = c.getAssets(); 

    InputStream in = null; 
    OutputStream out = null; 
    try 
    { 
     in = assetManager.open(filename); 
     String newFileName = sdcardpath/filename; 
     out = new FileOutputStream(newFileName); 

     byte[] buffer = new byte[1024]; 
     int read; 
     while ((read = in.read(buffer)) != -1) 
     { 
      out.write(buffer, 0, read); 
     } 
     in.close(); 
     in = null; 
     out.flush(); 
     out.close(); 
     out = null; 
    } catch (Exception e) { 
     Utility.printLog("tag", e.getMessage()); 
    }finally{ 
     if(in!=null){ 
      try { 
       in.close(); 
      } catch (IOException e) { 
       printLog(TAG, "Exception while closing input stream",e); 
      } 
     } 
     if(out!=null){ 
      try { 
       out.close(); 
      } catch (IOException e) { 
       printLog(TAG, "Exception while closing output stream",e); 
      } 
     } 
    } 
} 
+0

qstionで明記したように、コピーするだけのファイルではありませんが、フォルダとサブフォルダも – user1400538

+0

このリンクは、rawリソースからリソースをコピーするのに役立ちますhttp://stackoverflow.com/questions/939170/ resources-openrawresource-issue-android – vineet

+0

アセットからサブフォルダもコピーする必要がある場合は、コピーを少し修正する必要があります。 – vineet

関連する問題