通过使用Web服务,我们可以将文件从云下载到缓冲存储。我们现在需要从缓冲存储器访问这个下载的文件并将其移动到SD卡。我正在使用Eclipse和Android插件。
有人可以共享任何代码吗?
我的代码段在下方。
String filename = "Test.zip";
URL url = new URL(FromUrl);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.connect();
FileOutputStream fileOutput = openFileOutput(filename, Context.MODE_WORLD_READABLE);
InputStream inputStream = urlConnection.getInputStream();
int totalSize = urlConnection.getContentLength();
int downloadedSize = 0;
//create a buffer...
byte[] buffer = new byte[1024];
int bufferLength = 0; //used to store a temporary size of the buffer
//now, read through the input buffer and write the contents to the file
while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
fileOutput.write(buffer, 0, bufferLength);
downloadedSize += bufferLength;
}
//close the output stream when done
fileOutput.close();
return filename;
答案 0 :(得分:0)
只需保存到SD卡?
fileoutput = new FileOutputStream("/sdcard/"+filename);
这很快又脏,但是到目前为止找到的每个机器人都可以使用。 如果你想要有点整洁......
File ofile = new File(Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_DOWNLOADS),filename);
FileOutputStream f = new FileOutputStream(ofile);