今天接着學習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 <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 }
最后如果要正常運行的話,還要在AndroidManifest.xml文件中定義權限
1 <!--連接網絡的權限 -->
2 <uses-permission android:name="android.permission.INTERNET" />
3 <!-- 寫入存儲卡數據的權限 -->
4 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
沒有什么難理解的這節,我下去繼續學習...
哪位朋友給介紹個工作唄,要求北京,做android的,雖然我現在還沒有學完,但是相信我,我有很強的學習能力,然后就是動手能力,謝謝了