'Download a video to internal storage

We have a video streaming app and we want the user to be able to download videos to internal storage so he/she can watch offline. I'm very new to this and I've been looking for a way to do this. I first tried with Download Manager but then I found that you couldn't use that to download to internal storage.

I found the following using OkHttp:

    String url = "https://path_to_video.mp4";

    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder().url(url).build();
    client.newCall(request).enqueue(new Callback() {
        public void onFailure(Call call, IOException e) {
            e.printStackTrace();
        }

        public void onResponse(Call call, Response response) throws IOException {
            if (!response.isSuccessful()) {
                throw new IOException("Failed to download file: " + response);
            } else {
                Log.d(TAG, "Success");
            }
            String fileName = "filename.mp4";
            FileOutputStream fos = new FileOutputStream(fileName);
            fos.write(response.body().bytes());
            fos.close();
        }
    });

But even though I get the "Success" message the video doesn't appear to be downloading (If I check storage size for the app it clearly hasn't increased).

How can I manage to download the MP4 to the app's internal storage?

Additional information, I'm working within a fragment, with Java.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source