gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 21:03:46 25 4
gpt4 key购买 nike

这是我下载文件并将其保存在内部存储器中的代码:

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){

}
}

但是我必须如何更改代码,以便将文件保存在外部存储和应用程序私有(private)中?

最佳答案

使用 FileOutputStream fos = new FileOutputStream(getExternalFilesDir() + "/"+ fileName); 但这不是私有(private)内存。

关于android - 使用外部存储(私有(private))保存下载的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25167581/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com