Hey,
This is actually pretty basic and straightforward.
Firs you'll need to modify your manifext file to prompt for the
correct permissions for writing to the sdcard.
Also, reading from the sdcard can be slow. (at least from my experience.)
Downloading a file and saving it to the sdcard is pretty easy:
URL url = new URL(mUrl);
HttpURLConnection c = (HttpURLConnection)
url.openConnection();
c.connect();
File root = Environment.getExternalStorageDirectory();
File path = new File(root, "myfolder");
FileOutputStream f = new FileOutputStream(path);
InputStream in = c.getInputStream();
int len1 = 0;
byte[] buffer = new byte[1024];
while( ( len1 = in.read(buffer)) > 0 )
{
f.write(buffer, 0, len1);
}
f.flush();
f.close();
in.close();
Unzipping is just as easy:
try
{
BufferedOutputStream dest = null;
File root = Environment.getExternalStorageDirectory();
FileInputStream fis = new FileInputStream(new File(srcpath));
ZipInputStream zis = new ZipInputStream(new
BufferedInputStream(fis));
ZipEntry entry;
while((entry = zis.getNextEntry()) != null)
{
File file = new File(destpath, entry.getName());
int count;
byte data[] = new byte[1024];
FileOutputStream fos = new FileOutputStream(file.getPath());
//Log.d(TAG, "FOS: " + fos.toString());
dest = new BufferedOutputStream(fos, 1024);
while ( ( count = zis.read(data, 0, 1024) ) > 0 )
{
dest.write(data, 0, count);
}
dest.flush();
dest.close();
}
zis.close();
}
catch(IOException e)
{
Log.d(TAG, "Error extracting zip file: " + e.toString());
return false;
}
Not sure if that's the 100% correct way to do it, but it works really
well for me.
Miguel.
On Wed, Feb 3, 2010 at 9:07 PM, Android <[email protected]> wrote:
> I have created a rather large game, 18 megs, and am sick of getting 1
> star because of the size. Now normally I would just figure out how to
> do this on my own but since this is something I'm only doing because
> Google dropped the ball and may soon rectify, I would really
> appreciate it if some one would provide me with code to do the
> following.
>
> 1. Make an HTTP connection to a server and dowload a .zip file to the
> SD card.
> 2. Then unzip the file to a directory on the SD card.
> 3. Finally delete the .zip file.
>
> Has anyone done this before? What are the gotchas? I'm sure doing this
> comes at a cost, such as having to insure the graphics have been
> downloaded, failed downloads, and if you have a future update you have
> to manage updating the graphics yourself, am I missing anything?
>
> Thanks Very Much,
> Chad
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
--
http://diastrofunk.com, http://developingthedream.blogspot.com/,
http://www.youtube.com/user/revoltingx, ~Isaiah 55:8-9
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en