如何将原始文件夹中的音乐文件保存到android中的SD卡中

时间:2013-07-03 06:10:11

标签: android android-emulator

嗨我需要将音乐文件保存到SD卡中我尝试使用下面的代码只有文件夹在侧面文件夹中的sdcard创建没有音乐文件保存任何一个建议我在哪里我做错了...

  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();
            }
    }
    }
 }
 }

1 个答案:

答案 0 :(得分: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();
    }

希望这会对你有所帮助。