Android开发九:从网上下载文件并存储到SD卡中

来源:互联网 发布:淘宝分享链接怎么弄 编辑:程序博客网 时间:2024/05/22 10:23
 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     <Button
15 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     @Override
30     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 }
原创粉丝点击