android-async-http loopj set saving path for downloading file

3.8k views Asked by At

Please tell me how set saving path for downloading file.

For example:

AsyncHttpClient client = new AsyncHttpClient();
String[] allowedTypes = new String[] { "image/png" };
client.get("http://www.example.com/image.png", new BinaryHttpResponseHandler(allowedTypes) {
    @Override
    public void onSuccess(byte[] imageData) {
        // Successfully got a response
    }

    @Override
    public void onFailure(Throwable e, byte[] imageData) {
        // Response failed :(
    }
 });
1

There are 1 answers

0
Leonardo On BEST ANSWER

You need use this byte array with an OutputStream subclass to write the bytes on disc,

OutputStream f = new FileOutputStream(new File("path"));
f.write(bytes); //your bytes
f.close();

This code will write the file on disk, to get the path maintain the File on a variable and use File.getAbsolutePath()