2013-07-03 93 views
0

嗨,我需要將音樂文件保存到SD卡我試過使用下面的代碼只有文件夾在SD卡中創建的文件夾沒有音樂文件保存任何一個建議我在哪裏我做了錯誤...如何將音樂文件從原始文件夾保存到SD卡中android

FillesaveActivity.class: 
    public class FillesaveActivity extends Activity { 
/** Called when the activity is first created. */ 
public static ArrayList<Integer> list1 = new ArrayList<Integer>(); 
Integer[] text; 
int cnt; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    list1.add(R.raw.intro_letter_report_card); 
    list1.add(R.raw.intro_title_page2); 
    text = list1.toArray(new Integer[list1.size()]); 

    for(int i=0;i<text.length;i++) 
    { 
     cnt++; 


     File sd = Environment.getExternalStorageDirectory(); 
     File folDirectory = new File("/sdcard/Songsnew1/"); 
     // have the object build the directory structure, if needed. 
     folDirectory.mkdirs(); 
     File source = new File(""+text[i]); 
     File destination= new File(folDirectory, "song"+cnt+".mp3"); 
     if(source.exists()){ 
      try { 
       FileChannel src = new FileInputStream(source).getChannel(); 
       FileChannel dst = new FileOutputStream(destination).getChannel(); 
       dst.transferFrom(src, 0, src.size()); 
       src.close(); 
       dst.close(); 
      } catch (FileNotFoundException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
    } 
    } 
} 
} 

回答

2

使用這樣:

list1.add(R.raw.intro_letter_report_card); 
    list1.add(R.raw.intro_title_page2); 


    for(int name : list1) 
{ 
    cnt++; 
    InputStream stream = null; 

    File direct = new File(Environment.getExternalStorageDirectory() + "/Songsnew1"); 

     if(!direct.exists()) 
     { 
      if(direct.mkdir()); //directory is created; 

     } 


     stream = getResources().openRawResource(name); 



    String outFileName = Environment.getExternalStorageDirectory() + "/Songsnew1/song1"+cnt+".mp3"; 
     OutputStream myOutput = null; 


    try 
    { 
    myOutput = new FileOutputStream(outFileName); 
    } 
    catch (FileNotFoundException e2) 
    { 
    // TODO Auto-generated catch block 
    e2.printStackTrace(); 
    } 


    // transfer bytes from the input file to the output file 
    byte[] buffer = new byte[1024]; 
    int length; 

    try 
    { 
    while ((length = stream.read(buffer)) > 0) 
    { 
     try 
     { 
      myOutput.write(buffer, 0, length); 
     } 
     catch (IOException e) 
     { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     } 
    } 
    } 
    catch (IOException e1) 
    { 
    // TODO Auto-generated catch block 
    e1.printStackTrace(); 
     } 
    // Close the streams 
    try 
    { 
    myOutput.flush(); 
    } 
    catch (IOException e1) 
    { 
    // TODO Auto-generated catch block 
    e1.printStackTrace(); 
    } 
    try 
    { 
    myOutput.close(); 
    } 
    catch (IOException e1) 
    { 
    // TODO Auto-generated catch block 
    e1.printStackTrace(); 
    } 
    try 
    { 
    stream.close(); 
    } 
    catch (IOException e1) 
    { 
    // TODO Auto-generated catch block 
    e1.printStackTrace(); 
    } 

希望這會幫助你。

相關問題