Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.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_File_Download - Fatal编程技术网

Android 如何将文件下载到应用程序文件夹?

Android 如何将文件下载到应用程序文件夹?,android,file,download,Android,File,Download,我的代码只允许您保存下载到SD卡的文件,如何在应用程序本身中保存下载 守则: URL url = new URL(aurl[0]); URLConnection conexion = url.openConnection(); conexion.connect(); int lenghtOfFile = conexion.getContentLength(); Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile); InputStre

我的代码只允许您保存下载到SD卡的文件,如何在应用程序本身中保存下载

守则:

URL url = new URL(aurl[0]);
URLConnection conexion = url.openConnection();
conexion.connect();

int lenghtOfFile = conexion.getContentLength();
Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);

InputStream input = new BufferedInputStream(url.openStream());
FileOutputStream fos = openFileOutput("try.pdf", Context.MODE_PRIVATE);

byte data[] = new byte[1024];

long total = 0;

    while ((count = input.read(data)) != -1) {
        total += count;
        publishProgress(""+(int)((total*100)/lenghtOfFile));
        fos.write(data, 0, count);
    }

    fos.flush();
    fos.close();
    input.close();
} catch (Exception e) {}
return null;

}
应该只在另一个服务器上更改此路径吗

how could save the download within the application itself?
您需要清除它的含义,如果您想将其保存在原始或资产文件夹中,则不可能。您可以将文件存储在数据目录中

openFileOutput("car.pdf",mode)
模式可以是私有的、可编辑的、可写入的

依照

您可以将文件直接保存在设备的内部存储器上。默认情况下,保存到内部存储器的文件是应用程序的专用文件,其他应用程序无法访问它们(用户也不能)。当用户卸载应用程序时,这些文件将被删除

String FILENAME = "hello_file";
String string = "hello world!";

FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();

我已经更新了我的代码,我的代码部分应该替换为你的?或者它是无用的,应该重新开始?您可以编写fos.write(string.getBytes());在while循环内部,根据您的逻辑,您必须在您的示例中实现上述代码,该代码应包含字符串?好的,谢谢,我想我理解您想要告诉我的内容,但问题是该文件必须首先下载,这里的代码似乎是针对一个已经下载的文件的。你必须从inputsteam获取数据,并将其写在outputstream中。谢谢你对我的耐心,我已经更新了我的代码。你可以看到上面,我会试试看它是否有效