2014-02-19 64 views
0

我得到這段代碼將文件從App目錄複製到SD卡上的文件夾,並使用該文件設置爲鈴聲。 但有些東西我不明白。 如何確定要採用哪個文件? 假設我在原始文件夾中有一個名爲fusrodah的文件。 如何讓我的應用程序選擇該文件並將其複製到SD卡文件夾?如何選擇要複製的文件?

private int size; 

private static final int BUFFER_LEN = 1024; 


private void copyFile(AssetManager assetManager, String fileName, File out) throws FileNotFoundException, IOException { 
    size = 0; 
    FileOutputStream fos = new FileOutputStream(out); 
    InputStream is = assetManager.open(fileName);  
    int read = 0; 
    byte[] buffer = new byte[BUFFER_LEN]; 
    while ((read = is.read(buffer, 0, BUFFER_LEN)) >= 0) { 
      fos.write(buffer, 0, read); 
      size += read; 
     } 
    fos.flush();  
    fos.close(); 
    is.close(); 
} 
public void onClick(View arg0) {   
    AssetManager assetManager = getAssets(); 

    File file = new File(Environment.getExternalStorageDirectory(), 
      "/myRingtonFolder/Audio/"); 
    if (!file.exists()) { 
     file.mkdirs(); 
    } 

    String path = Environment.getExternalStorageDirectory() 
      .getAbsolutePath() + "/myRingtonFolder/Audio/"; 

    File out = new File(path + "/", "fusrodah.mp3");  
    if(!out.exists()){ 
     try { 
      copyFile(assetManager, "fusrodah.mp3", out); 
     } catch (FileNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

回答

1

替換此

InputStream is = assetManager.open(fileName);  

InputStream is = getResources().openRawResource(R.raw.fusrodah); 
+0

相關問題:http://stackoverflow.com/questions/4081763/access-resource-files-in-android – indivisible