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

이 링크는 원시 리소스 (http://stackoverflow.com/questions/939170/)에서 리소스를 복사하는 데 도움이됩니다. resources-openrawresource-issue-android – vineet

+0

자산에서 하위 폴더도 복사해야하는 경우 복사를 약간 수정해야합니다. – vineet

관련 문제