Laire
Laire

Reputation: 1330

Save downloaded file using the External Storage (private)

this is my code to download a file and save it in the internal storage:

public class getResults extends AsyncTask<String, Void, String>{
    @Override
    protected String doInBackground(String... params){
        String fileName = "results"+month+year+".pdf";
        URLConnection conn;
        try {
            URL url = new URL(params[0]);
            conn = url.openConnection();
            int contentLength = conn.getContentLength();
            DataInputStream in = new DataInputStream(conn.getInputStream());

            byte[] buffer = new byte[contentLength];
            in.readFully(buffer);
            in.close();

            if (buffer.length > 0) {
                DataOutputStream out;
                FileOutputStream fos = openFileOutput(fileName,Context.MODE_PRIVATE);
                out = new DataOutputStream(fos);
                out.write(buffer);
                out.flush();
                out.close();
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "true";
    }

    protected void onPostExecute(String result){

    }
}

but how I have to change the code, so that I save the file on External Storage and app-private?

Upvotes: 1

Views: 1179

Answers (1)

greenapps
greenapps

Reputation: 11224

Use FileOutputStream fos = new FileOutputStream(getExternalFilesDir() + "/" + fileName); But this is not private memory.

Upvotes: 2

Related Questions