简体   繁体   English

如何将music.mp3文件下载到android中的应用存储?

[英]How to download music .mp3 files to app storage in android?

Music files should be downloaded to the app itself, it shouldn't get stored on mobile memory (file manager or SD card).音乐文件应该下载到应用程序本身,它不应该存储在移动 memory(文件管理器或 SD 卡)上。

FileManager Class to store mp3 file inside app folders. FileManager Class 将 mp3 文件存储在应用程序文件夹中。

public class FileManager {

    private final Context context;

    FileManager(Context context) {
        this.context = context;
    }

    public String getRootPath() {
    File file = new File(context.getFilesDir(),"");
    return file.getAbsolutePath();
}
public String getFilePath(String name) {
        File file = new File(context.getFilesDir(), name);
        return file.getAbsolutePath();
    }
  }

Download request下载请求

@RequiresApi(api = Build.VERSION_CODES.O)
    private void initDownload(String soundUri) {
        Log.d(TAG, "initDownload: " + soundUri);

        Retrofit retrofit = new Retrofit.Builder().baseUrl(BASE_URL).build();

        ApiService retrofitInterface = retrofit.create(ApiService.class);
        Call<ResponseBody> request = retrofitInterface.downloadFile(soundUri);

        request.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
               if (response.body() != null) {
                    Thread thread = new Thread(() -> {
                        try {
                            saveVoice(response.body());
                        } catch (IOException e) {
                            Log.d(TAG, "onResponse: " + e);
                        }
                    });
                    thread.start();
                }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {

        }
    });
    }




@RequiresApi(api = Build.VERSION_CODES.O)
private void saveVoice(ResponseBody body) throws IOException {
    try {
        String mp3Key= "YOUR_KEY_TO_STORE_FILE";

        int count;
        byte data[] = new byte[1024 * 4];
        InputStream bis = new BufferedInputStream(body.byteStream(), 1024 * 8);
        File outputFile = new File(fileManager.getRootPath(), voiceKey);
        OutputStream output = new FileOutputStream(outputFile);
        long startTime = System.currentTimeMillis();
        int timeCount = 1;
        while ((count = bis.read(data)) != -1) {

            long currentTime = System.currentTimeMillis() - startTime;

            if (currentTime > 1000 * timeCount) {

                timeCount++;
            }
            output.write(data, 0, count);
        }
        output.flush();
        output.close();
        bis.close();
    } catch (Exception e) {
        Log.d(TAG, "saveVoice: " + e);
    }
}

After successfully download you can read file with the key that you wrote file to app storage.成功下载后,您可以使用您将文件写入应用存储的密钥读取文件。 To do that:要做到这一点:

String filePath = fileManager.getFilePath(mp3Key); // filePath returns local url of file

For example, you can do:例如,您可以这样做:

 mediaPlayer.setDataSource(filePath );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM