Hi i try to download 3gpp file from server
@GET("/{filename}")
@Streaming
public void getFileFromServer(
@Path("filename") String filename,
Callback<Response> callback
);
and my calling code is
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint("http://xxx.xxx.xx.xx/api/") //call your base url
.build();
MyApi myapi = restAdapter.create(MyApi.class);
myApi.getFileFromServer(Filename, new Callback<Response>() {
@Override
public void success(Response response, Response response1) {
Log.w("CMS Return", "");
File direct = new File(Environment.getExternalStorageDirectory() + Constants.LocalImageSaveLastStringDirectory + Constants.LocalVoiceSaveLastStringDirectory);
if (!direct.exists()) {
File wallpaperDirectory = new File(Constants.LocalvoiceSaveFullDirectory);
wallpaperDirectory.mkdirs();
}
String fileName1 = getLastWord(Filename);
File pdfFile = new File(new File(Constants.LocalvoiceSaveFullDirectory), fileName1);
if (pdfFile.exists()) {
pdfFile.delete();
}
FileOutputStream output = null;
try {
output = new FileOutputStream(pdfFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
TypedInput body = response.getBody();
String bodyMime = body.mimeType();
byte[] bodyBytes = streamToBytes(body.in());
org.apache.commons.io.IOUtils.write(bodyBytes, output);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void failure(RetrofitError error) {
System.out.println("Check retrofit Value" + error);
}
});
Stream to bytes method
private static byte[] streamToBytes(InputStream stream) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (stream != null) {
byte[] buf = new byte[BUFFER_SIZE];
int r;
while ((r = stream.read(buf)) != -1) {
baos.write(buf, 0, r);
}
}
return baos.toByteArray();
}
And i got crash on stream.read(buf)) i debug this issue network main thread issue how to solve or how to download 3gp voice file from server