Updated 14 October 2019
The files in android can be saved in either internal and external memories.By default, files saved to the internal storage are private to your application and other applications cannot access them (nor can the user). When the user uninstalls your application, these files are removed.
While files stored to the external storage are shareable and can be modified.
That is why the files that are specific for the application for example, additional resources downloaded by your app or temporary media files, are saved in internal storage. There are various ways to download a file but here we are going to download the file using volley.
Firstly you have to create your own custom request class like
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
class InputStreamVolleyRequest extends Request<byte[]> { private final Response.Listener<byte[]> mListener; private Map<String, String> mParams; //create a static map for directly accessing headers public Map<String, String> responseHeaders ; public InputStreamVolleyRequest(int method, String mUrl ,Response.Listener<byte[]> listener, Response.ErrorListener errorListener, HashMap<String, String> params) { // TODO Auto-generated constructor stub super(method, mUrl, errorListener); // this request would never use cache. setShouldCache(false); mListener = listener; mParams=params; } @Override protected Map<String, String> getParams() throws com.android.volley.AuthFailureError { return mParams; }; @Override protected void deliverResponse(byte[] response) { mListener.onResponse(response); } @Override protected Response<byte[]> parseNetworkResponse(NetworkResponse response) { //Initialise local responseHeaders map with response headers received responseHeaders = response.headers; //Pass the response data here return Response.success( response.data, HttpHeaderParser.parseCacheHeaders(response)); } } |
Now just send request though our custom class with Request.Method.GET and the url from where you want to doenload the file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
String mUrl= <YOUR_URL>; InputStreamVolleyRequest request = new InputStreamVolleyRequest(Request.Method.GET, mUrl, new Response.Listener<byte[]>() { @Override public void onResponse(byte[] response) { // TODO handle the response try { if (response!=null) {              FileOutputStream outputStream;              String name=<FILE_NAME_WITH_EXTENSION e.g reference.txt>;                outputStream = openFileOutput(name, Context.MODE_PRIVATE);                outputStream.write(response);                outputStream.close();                Toast.makeText(this, "Download complete.", Toast.LENGTH_LONG).show();            }        } catch (Exception e) {            // TODO Auto-generated catch block            Log.d("KEY_ERROR", "UNABLE TO DOWNLOAD FILE");            e.printStackTrace();        }  } } ,new Response.ErrorListener() {  @Override  public void onErrorResponse(VolleyError error) {    // TODO handle the error    error.printStackTrace();  } }, null);          RequestQueue mRequestQueue = Volley.newRequestQueue(getApplicationContext(), new HurlStack());          mRequestQueue.add(request); |
Now go to your application folder data/data/<your_application>/<file_name> and there is your file you can also download the file to external stoage.
You can access the files in internal storage using
1 |
Context.getFilesDir().<file_name> |
It returns the file with that name from the internal directory of application and null if there is no file with such name. Dont forget to add extenion of the file with the name.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
16 comments
Works fine
I will correct the same in the content right away.
You can pass params while calling the constructor of InputStreamVolleyRequest()
Hey jyothi,
Do you wish to save the response to a pdf file or do you wish to download a file and save that as pdf ?
If you wish to save the response in a pdf file then you will need a custom library that allows the creation and editing of pdf files and that is actually a complete another development phase.
If you wish to just download a file and then save it as a pdf, then we can just keep any name in the variable name as mentioned in the blog itself.
E,g: you wish to save a file as “Test.pdf” then just change the value of
String name=”Test.pdf”;
and move ahead with steps and codes mentioned in the blog itself.
I hope this helps.