0

嗨,我需要将音乐文件数组保存到 sdcard 中,我尝试使用下面的代码,只有文件夹在侧文件夹的 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();
            }
    }
    }
 }
 }
4

1 回答 1

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

希望这会帮助你。

于 2013-07-03T06:17:57.647 回答