標籤:byte list ade lis btn arm ror drawable 返回
想把檔案儲存到SD卡中,一定要知道SD卡的路徑,擷取SD卡路徑:
Environment.getExternalStorageDirectory()
另外,在儲存之前要判斷SD卡是否已經安裝好,並且可讀寫:
//判斷SDcard是否存在並且可讀寫if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ service.saveToSDCard(filename,filecontent); Toast.makeText(getApplicationContext(), R.string.success, 1).show(); }else{ Toast.makeText(getApplicationContext(), R.string.sdcarderror, 1).show(); }
執行個體
1. 下載存文字檔和下載如mp3等大容量的檔案
2.代碼編寫
AndroidMainfest.xml中配置
主要是解決網路許可權和寫SDCard的許可權
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.jingling.practice.downloadtest" > <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
package util;import android.util.Log;import java.io.BufferedReader;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URL;/** * Created by Spring on 2015/11/7. * 下載工具類 */public class HttpDownloader { private URL url = null; private final String TAG = "TAG"; /** * 讀取文字檔 * @param urlStr url路徑 * @return 文本資訊 * 根據url下載檔案,前提是這個檔案中的內容是文本, * 1.建立一個URL對象 * 2.通過URL對象,建立一個Http串連 * 3.得到InputStream * 4.從InputStream中得到資料 */ public String download(String urlStr) { StringBuffer sb = new StringBuffer(); String line = null; BufferedReader bufferedReader = null; try { url = new URL(urlStr); //建立http串連 HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); //使用IO流讀取資料 bufferedReader = new BufferedReader(new InputStreamReader(urlConn.getInputStream())); while ((line = bufferedReader.readLine()) != null) { sb.append(line); } } catch (IOException e) { e.printStackTrace(); } finally { try { bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } } Log.e("TAG","下載txt檔案"); Log.e("TAG",sb.toString()); return sb.toString(); } /** * 讀取任何檔案 * 返回-1 ,代表下載失敗。返回0,代表成功。返回1代表檔案已經存在 * * @param urlStr * @param path * @param fileName * @return */ public int downlaodFile(String urlStr, String path, String fileName) { InputStream input = null; try { FileUtil fileUtil = new FileUtil(); if (fileUtil.isFileExist(path + fileName)) { return 1; } else { input = getInputStearmFormUrl(urlStr); File resultFile = fileUtil.write2SDFromInput(path,fileName,input); if (resultFile == null) return -1; } } catch (IOException e) { e.printStackTrace(); return -1; } finally { try { input.close(); } catch (IOException e) { e.printStackTrace(); } } return 0; } public InputStream getInputStearmFormUrl(String urlStr) throws IOException { url = new URL(urlStr); HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); InputStream input = urlConn.getInputStream(); return input; }}
FileUtil.java
package util;import android.os.Environment;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;/** * Created by Spring on 2015/11/7. */public class FileUtil { private String SDPATH; public FileUtil() { } public String getSDPATH() { return SDPATH; } public FileUtil(String SDPATH){ //得到外部存放裝置的目錄(/SDCARD) SDPATH = Environment.getExternalStorageDirectory() + "/" ; } /** * 在SD卡上建立檔案 * @param fileName * @return * @throws java.io.IOException */ public File createSDFile(String fileName) throws IOException { File file = new File(SDPATH + fileName); file.createNewFile(); return file; } /** * 在SD卡上建立目錄 * @param dirName 目錄名字 * @return 檔案目錄 */ public File createDir(String dirName){ File dir = new File(SDPATH + dirName); dir.mkdir(); return dir; } /** * 判斷檔案是否存在 * @param fileName * @return */ public boolean isFileExist(String fileName){ File file = new File(SDPATH + fileName); return file.exists(); } public File write2SDFromInput(String path,String fileName,InputStream input){ File file = null; OutputStream output = null; try { createDir(path); file =createSDFile(path + fileName); output = new FileOutputStream(file); byte [] buffer = new byte[4 * 1024]; while(input.read(buffer) != -1){ output.write(buffer); output.flush(); } } catch (IOException e) { e.printStackTrace(); } finally { try { output.close(); } catch (IOException e) { e.printStackTrace(); } } return file; }}
3.MainActivity
package com.jingling.practice.downloadtest;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.Button;import util.HttpDownloader;public class MainActivity extends Activity { HttpDownloader downloader ; private Button btn_downFile; private Button btn_downMP3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_downFile = (Button)this.findViewById(R.id.button); btn_downMP3 = (Button)this.findViewById(R.id.button2); btn_downFile.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { downloader = new HttpDownloader(); String urlStr = "http://172.17.54.91:8080/download/down.txt"; downloader.download(urlStr); } }); btn_downMP3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String urlStr="http://172.17.54.91:8080/download/1.mp3"; String path="file"; String fileName="2.mp3"; HttpDownloader httpDownloader = new HttpDownloader(); httpDownloader.downlaodFile(urlStr,path,fileName); } }); }}
Android HTTP下載檔案並儲存到本地或SD卡