1、从网上下载文件
Android 2.3 以后的系统可以使用 DownloadManager,但是以前的系统必须自己写下载文件的代码。
这段代码如下:
try { //set the download URL, a url that points to a file on the internet //this is the file to be downloaded URL url = new URL("http://somewhere.com/some/webhosted/file"); //create the new connection HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); //set up some things on the connection urlConnection.setRequestMethod("GET"); urlConnection.setDoOutput(true); //and connect! urlConnection.connect(); //set the path where we want to save the file //in this case, going to save it on the root directory of the //sd card. File SDCardRoot = Environment.getExternalStorageDirectory(); //create a new file, specifying the path, and the filename //which we want to save the file as. File file = new File(SDCardRoot,"somefile.ext"); //this will be used to write the downloaded data into the file we created FileOutputStream fileOutput = new FileOutputStream(file); //this will be used in reading the data from the internet InputStream inputStream = urlConnection.getInputStream(); //this is the total size of the file int totalSize = urlConnection.getContentLength(); //variable to store total downloaded bytes 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 ) { //add the data in the buffer to the file in the file output stream (the file on the sd card fileOutput.write(buffer, 0, bufferLength); //add up the size so we know how much is downloaded downloadedSize += bufferLength; //this is where you would do something to report the prgress, like this maybe updateProgress(downloadedSize, totalSize); } //close the output stream when done fileOutput.close(); //catch some possible errors... } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // see http://androidsnippets.com/download-an-http-file-to-sdcard-with-progress-notification
2、对文件进行解压缩
import android.util.Log; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; /** * * @author jon */ public class Decompress { private String _zipFile; private String _location; public Decompress(String zipFile, String location) { _zipFile = zipFile; _location = location; _dirChecker(""); } public void unzip() { try { FileInputStream fin = new FileInputStream(_zipFile); ZipInputStream zin = new ZipInputStream(fin); ZipEntry ze = null; while ((ze = zin.getNextEntry()) != null) { Log.v("Decompress", "Unzipping " + ze.getName()); if(ze.isDirectory()) { _dirChecker(ze.getName()); } else { FileOutputStream fout = new FileOutputStream(_location + ze.getName()); for (int c = zin.read(); c != -1; c = zin.read()) { fout.write(c); } zin.closeEntry(); fout.close(); } } zin.close(); } catch(Exception e) { Log.e("Decompress", "unzip", e); } } private void _dirChecker(String dir) { File f = new File(_location + dir); if(!f.isDirectory()) { f.mkdirs(); } } }
有用的资源:
http://www.java-samples.com/showtutorial.php?tutorialid=1521 下载文件、显示进度
http://www.jondev.net/articles/Unzipping_Files_with_Android_%28Programmatically%29 解压文件
3、访问数据库的步骤
1)定义contract class(就是包含一些常量的类,保存表名,列名等),同时implements BaseColumns。
2)subclass SQLiteOpenHelper,实现几个onCreate() onUpgrade() onDowngrade()
3)创建SQLiteOpenHelper子类的对象,调用 getWritableDatabase() / getReadableDatabase(),获取SQLiteDatabase接口
4)使用SQLiteDatabase接口对数据库执行各种操作。