Android 常用代码收集
来源:互联网 发布:人工智能第二版答案 编辑:程序博客网 时间:2024/05/15 23:53
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 downloadedURL url = new URL("http://somewhere.com/some/webhosted/file");//create the new connectionHttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();//set up some things on the connectionurlConnection.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 createdFileOutputStream fileOutput = new FileOutputStream(file);//this will be used in reading the data from the internetInputStream inputStream = urlConnection.getInputStream();//this is the total size of the fileint totalSize = urlConnection.getContentLength();//variable to store total downloaded bytesint 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 filewhile ( (bufferLength = inputStream.read(buffer)) > 0 ) {//add the data in the buffer to the file in the file output stream (the file on the sd cardfileOutput.write(buffer, 0, bufferLength);//add up the size so we know how much is downloadeddownloadedSize += bufferLength;//this is where you would do something to report the prgress, like this maybeupdateProgress(downloadedSize, totalSize);}//close the output stream when donefileOutput.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
注意:下载文件的任务要放到主线程以外去执行,例如可以使用 AsyncTask。上面代码的出处在末尾注释行。
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接口对数据库执行各种操作。
- Android 常用代码收集
- android常用代码收集
- Android之常用代码片段收集
- 收集大家常用的一些Android代码
- 收集一些Android常用混淆代码
- 收集了一些常用的Android代码
- 常用JS代码收集
- 常用代码收集...
- 常用JS代码收集
- 常用代码收集
- Flex 常用代码收集
- JS 常用代码收集
- JS常用代码收集
- 收集网站常用代码
- 常用JS代码收集
- JQuery 常用代码收集
- 常用javascript代码收集
- JQuery 常用代码收集
- Ext3.3 中chart 报表显示有关问题
- MUILIB-一个可以实现界面自由配置的C++高级界面库
- flex4+spring整合有关问题,Unexpected child element 'factory' found in 'destination' from
- 怎样实现点击button查询从数据库中找出符合条件的结果。。。求问(以下代码可以否)
- Hubbub: 号称第一的语音社交网站上线
- Android 常用代码收集
- 远程调用 比较 jboss server
- 关于弹出div层居中的问题及解决方法
- “此致敬礼”是什么意思
- POJ 1989 The Cow Lineup [dp]
- Struts2简单配置国际化资源文件及用法
- SELinux false alarm for some utilities
- 利用CodeIgniter中的Email类发邮件
- 果粉福音: 金士顿发布Wi-Drive扩展IOS设备存储