1

我想将一个 mp3 文件从我的应用程序原始文件夹复制到/mnt/SDcard,但我不知道这项工作。

这是不可能的吗?

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

谢谢。

4

3 回答 3

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();
}
于 2013-02-03T10:10:15.360 回答
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

于 2013-03-13T16:40:08.923 回答
0

看看这个问题并替换assetsraw.

如何将文件从“资产”文件夹复制到 SD 卡?

于 2013-02-03T10:10:08.433 回答