Android开发九:从网上下载文件并存储到SD卡中
今天接着学习android下载文件,这几天没有时间,要准备找工作了,还没有学好呢,而且白天还有别的事忙。代码的讲解就很少了,但是重要的部分都有注释的,也不难理解,因为这种情况,博客也不往首页挂了...
直接进入正题,布局文件里面有一个文本框用来输入网址,我写了个默认的放里边了,百度的LOGO。后面就是一个下载按钮- 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:orientation="vertical"> 6 7 <EditText 8 android:text="http://www.baidu.com/img/baidu_jgylogo3.gif" 9 android:id="@+id/editText1"10 android:layout_width="fill_parent"11 android:layout_height="wrap_content"12 android:inputType="textUri"></EditText>13 14 <Button15 android:id="@+id/button1"16 android:layout_width="wrap_content"17 android:layout_height="wrap_content"18 android:text="@string/downbtn"19 android:layout_gravity="center" />20 21 </LinearLayout>
- 复制代码
复制代码 下面看代码就明白了,都注释上了- 1 package com.yyj.DownloadAndStore; 2 3 import java.io.File; 4 import java.io.FileOutputStream; 5 import java.io.InputStream; 6 import java.io.OutputStream; 7 import java.net.HttpURLConnection; 8 import java.net.URL; 9 10 11 import android.app.Activity;12 import android.os.Bundle;13 import android.os.Environment;14 import android.view.View;15 import android.view.View.OnClickListener;16 import android.widget.Button;17 import android.widget.EditText;18 import android.widget.Toast;19 20 public class DownloadAndStoreActivity extends Activity {21 /** Called when the activity is first created. */22 Button button;23 EditText editText;24 URL url;25 //获取SDCard根目录26 String sdcard=Environment.getExternalStorageDirectory()+"/";27 //这个是要保存的目录28 String filepath=sdcard+"yyjdownload/";29 @Override30 public void onCreate(Bundle savedInstanceState) {31 super.onCreate(savedInstanceState);32 setContentView(R.layout.main);33 34 editText=(EditText)findViewById(R.id.editText1);35 button=(Button)findViewById(R.id.button1);36 button.setOnClickListener(new OnClickListener() { 37 public void onClick(View v) {38 String urlString=editText.getText().toString();39 //url字符串,如果前面不加http://会异常,这里不考虑ftp情况40 urlString=(urlString.startsWith("http://"))?urlString:"http://"+urlString;41 try {42 url=new URL(urlString);43 //打开到url的连接44 HttpURLConnection connection = (HttpURLConnection)url.openConnection();45 //以下为java IO部分,大体来说就是先检查文件夹是否存在,不存在则创建,然后的文件名重复问题,没有考虑46 InputStream istream=connection.getInputStream();47 String filename=urlString.substring(urlString.lastIndexOf("/")+1);48 49 File dir=new File(filepath);50 if (!dir.exists()) {51 dir.mkdir();52 }53 File file=new File(filepath+filename);54 file.createNewFile();55 56 OutputStream output=new FileOutputStream(file);57 byte[] buffer=new byte[1024*4];58 while (istream.read(buffer)!=-1) {59 output.write(buffer);60 }61 output.flush();62 output.close();63 istream.close();64 //最后toast出文件名,因为这个程序是单线程的,所以要下载完文件以后才会执行这一句,中间的时间类似于死机,不过多线程还没有学到65 Toast.makeText(DownloadAndStoreActivity.this, filename, Toast.LENGTH_LONG).show();66 } catch (Exception e) {67 e.printStackTrace();68 }69 70 }71 });72 } 73 }
- 复制代码
复制代码 |