0

I am trying to download an audio .wav file from AWS using OkHTTP.

Problem

API is executing. It is giving no error, but the downloaded file size is 660B while it should be 61.92KB. When I try to download the audio from my browser using same API link, it is downloaded, however using android mobile the audio size is in bits and full audio is not downloaded.

Below is code for Downloading file

Request request = new Request.Builder()
                    .url(audio_url)
                    .build();

AwsInterceptor awsInterceptor = new AwsInterceptor(new AWSCredentialsProvider() {
   @Override
   public AWSCredentials getCredentials() {
     return new BasicAWSCredentials(access_key_id, secret_access_key);
   }

   @Override
   public void refresh() {

   }
}, API_GATEWAY_SERVICE_NAME, region);


OkHttpClient client = new OkHttpClient.Builder()
                    .addInterceptor(awsInterceptor)
                    .build();

client.newCall(request).enqueue(new Callback() {
    @Override
    public void onFailure(Call call, IOException e) {
        
          String mMessage = e.getMessage();
          Log.e("failure Response", mMessage);
          call.cancel();

   }

     @Override
     public void onResponse(Call call, Response response) throws IOException {

         byte[] rawData = response.body().bytes();
         File recordingFile = new File(dir_path, file_name+".wav");
         bytesToWav(recordingFile, rawData);

         /*try (BufferedSource bufferedSource = response.body().source()) {
              BufferedSink bufferedSink = Okio.buffer(Okio.sink(recordingFile ));
              bufferedSink.writeAll(bufferedSource);
              bufferedSink.close();
        }*/

     }
});

Solutions I tried

I tried multiple solutions like downloading using okio but didn't work.

I tried stackoverflow solution1, solution2, solution3, solution4 but nothing worked for me. I cant figure out what the problem is.

Also if there is any other way to download .wav file please let me know.

Umama Khalid
  • 167
  • 1
  • 16
  • The downloaded file is contained in `byte[] rawData = response.body().bytes();` So its size would be `rawData.length`. Is that zero? If not then how many bytes? – blackapps Apr 26 '21 at 08:50
  • rawData.length() shows 660B. when i convert it to .wav then it become 704B. but when I apply solution 4 then the size remain 0B. the original size of file is 61.92KB – Umama Khalid Apr 26 '21 at 09:03
  • `rawData.length() shows 660B` Wel... where is your comment on these few bytes? It should be 61.92KB but how many bytes is 61.92KB ? – blackapps Apr 26 '21 at 09:37
  • 61920B. With 660B we can not play the audio. Which means files is missing bytes. I am so confused. Why it is missing those bytes and how can I get them? – Umama Khalid Apr 26 '21 at 09:58
  • https://stackoverflow.com/a/29012988/115145 is endorsed by OkHttp's developers. – CommonsWare Apr 26 '21 at 10:56
  • I also tried this solution but still bytes are 660. how to get all bytes? is there any problem in my code? or in android version? – Umama Khalid Apr 27 '21 at 06:24
  • Facing something similar. Did you figure our the solution? – user13628417 Mar 10 '22 at 09:28
  • Yes I found the solution. Will share it. – Umama Khalid May 10 '22 at 10:55

0 Answers0