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.