从uri下载歌曲并将其存储在应用程序特定的文件夹中

时间:2018-09-02 05:43:20

标签: android

我正在尝试使用URI从服务器下载歌曲并将其存储在res / raw文件夹中。我该如何在android studio中做到这一点。

1 个答案:

答案 0 :(得分:0)

您不能将mp3或任何文件存储在res / raw文件夹中,仅当apk制成read this时,它才是只读的, 您可以将文件存储在手机存储中。

代码:

      try{

      File newFile = newFile(android.os.Environment.getExternalStorageDirectory(),"Your Folder Name");
      if(!newFile.exists())
      newFile.mkdirs();

      File f=new File(newFile,songname+".mp3");
      URL url = new URL(yourSongUrl); 

        InputStream input = new BufferedInputStream(url.openStream());
        OutputStream output = new FileOutputStream(f);

        byte data[] = new byte[1024];
        long total = 0;
        int count=0;
        while ((count = input.read(data)) != -1) {
            total++;
            Log.e("while","A"+total);

            output.write(data, 0, count);
        }

        output.flush();
        output.close();
        input.close();
     }
     Catch(Exception e){e.printStackTrace();}
相关问题