这是我的代码,用于下载文件并将其保存在内部存储中:
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){
}
}
但是我该如何更改代码,以便将文件保存在外部存储中并由应用专用?
最佳答案
使用FileOutputStream fos = new FileOutputStream(getExternalFilesDir() + "/" + fileName);
但这不是私有内存。
关于android - 使用外部存储(专用)保存下载的文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25167581/