Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/205.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用外部存储器保存下载的文件(专用)_Android_Download_Storage_Android External Storage - Fatal编程技术网

Android 使用外部存储器保存下载的文件(专用)

Android 使用外部存储器保存下载的文件(专用),android,download,storage,android-external-storage,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;

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

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

    }
}
public类getResults扩展异步任务{
@凌驾
受保护的字符串doInBackground(字符串…参数){
String fileName=“results”+月+年+“.pdf”;
连接连接;
试一试{
URL=新URL(参数[0]);
conn=url.openConnection();
int contentLength=conn.getContentLength();
DataInputStream in=新的DataInputStream(conn.getInputStream());
字节[]缓冲区=新字节[contentLength];
in.readfull(缓冲区);
in.close();
如果(buffer.length>0){
数据输出流输出;
FileOutputStream fos=openFileOutput(文件名,Context.MODE\u PRIVATE);
out=新数据输出流(fos);
输出。写入(缓冲区);
out.flush();
out.close();
}
}捕获(格式错误){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
返回“真”;
}
受保护的void onPostExecute(字符串结果){
}
}

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

使用
FileOutputStream fos=newfileoutputstream(getExternalFilesDir()+“/”+fileName)但这不是私有内存。

您希望实际保存在哪里,外部目录还是应用程序内部目录??