我想将歌曲复制到内部存储中的其他文件夹

时间:2019-08-12 05:24:19

标签: android android-studio

我有File Song(这是其他活动中的song.mp3文件),我想将其保存在内部存储中的特定文件夹中。

     protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_playlist);

        Bundle bundle = getIntent().getExtras();
        File song = (File)bundle.get("playlist");


    }

1 个答案:

答案 0 :(得分:0)

首先,您需要将所有内容包装在try / catch中,并记录异常块,以借助exception.getMessage()来找出错误所在

以下是复制文件的示例

 InputStream inStream = null;
    OutputStream outStream = null;

    try{

        File afile =new File("C:\\foldeOne\\Afile.txt");
        File bfile =new File("C:\\foldeTwo\\Afile.txt");

        inStream = new FileInputStream(afile);
        outStream = new FileOutputStream(bfile);

        byte[] buffer = new byte[1024];

        int length;
        //copy the file content in bytes
        while ((length = inStream.read(buffer)) > 0){

            outStream.write(buffer, 0, length);

        }

        inStream.close();
        outStream.close();

        //delete the original file
        afile.delete();

        System.out.println("File is copied successful!");

    }catch(IOException e){
        //Here you suppose to handle exception to figure out what went wrong
        e.printStackTrace();
    }
}

原始物在这里https://www.mkyong.com/java/how-to-move-file-to-another-directory-in-java/