使用外部存储保存下载的文件(私有)

时间:2014-08-06 18:30:35

标签: android download storage android-external-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){

    }
}

但我如何更改代码,以便将文件保存在外部存储和app-private上?

1 个答案:

答案 0 :(得分:2)

使用FileOutputStream fos = new FileOutputStream(getExternalFilesDir() + "/" + fileName);但这不是私人记忆。