Android(安卓)常用代码收集
16lz
2021-01-25
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接口对数据库执行各种操作。
更多相关文章
- 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
- 一款常用的 Squid 日志分析工具
- GitHub 标星 8K+!一款开源替代 ls 的工具你值得拥有!
- RHEL 6 下 DHCP+TFTP+FTP+PXE+Kickstart 实现无人值守安装
- Linux 环境下实战 Rsync 备份工具及配置 rsync+inotify 实时同步
- 腾讯云-视频直播(android集成)
- Android中常用的List滚动列表操作
- Android代码规范+小技巧
- android基于Gps 定位和基站定位获取经纬度