2013-02-03 63 views
1

我想从我的应用程序原料文件夹复制MP3文件到到/ mnt/SD卡但我不知道这份工作。从原材料目录将文件复制到SD卡

这是不可能的?

如果您有任何答案告诉我有关这些代码需要的权限;

thanx。

+0

你将需要添加SD卡AndroidManifest的许可 –

+0

是的,我知道。但这份工作的方式(复制文件)是个问题。 –

回答

2

这里是你可以用什么来做到这一点:

InputStream in = getResources().openRawResource(R.raw.myresource); 
FileOutputStream out = new FileOutputStream(somePathOnSdCard); 
byte[] buff = new byte[1024]; 
int read = 0; 

try { 
    while ((read = in.read(buff)) > 0) { 
     out.write(buff, 0, read); 
    } 
} finally { 
    in.close(); 

    out.close(); 
} 
2

试试这个方法

/** 
* @param sourceLocation like this /mnt/sdcard/XXXX/XXXXX/15838e85-066d-4738-a243-76c461cd8b01.jpg 
* @param destLocation /mnt/sdcard/XXXX/XXXXX/15838e85-066d-4738-a243-76c461cd8b01.jpg 
* @return true if successful copy file and false othrerwise 
* 
* set this permissions in your application WRITE_EXTERNAL_STORAGE ,READ_EXTERNAL_STORAGE 
* 
*/ 
public static boolean copyFile(String sourceLocation, String destLocation) { 
    try { 
     File sd = Environment.getExternalStorageDirectory(); 
     if(sd.canWrite()){ 
      File source=new File(sourceLocation); 
      File dest=new File(destLocation); 
      if(!dest.exists()){ 
       dest.createNewFile(); 
      } 
      if(source.exists()){ 
       InputStream src=new FileInputStream(source); 
       OutputStream dst=new FileOutputStream(dest); 
       // Copy the bits from instream to outstream 
       byte[] buf = new byte[1024]; 
       int len; 
       while ((len = src.read(buf)) > 0) { 
        dst.write(buf, 0, len); 
       } 
       src.close(); 
       dst.close(); 
      } 
     } 
     return true; 
    } catch (Exception ex) { 
     ex.printStackTrace(); 
     return false; 
    } 
} 

更多信息请访问AndroidGuide

相关问题