程式想要訪問網路 OR 寫入檔到SD卡 需要許可權

在AndroidManifest.xml 中添加:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

 

放在application 標籤之外的
一般就放在</manifest>之前就Ok




Activity:Download ==================================================
package mars.download;

 

import mars.utils.HttpDownloader;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

 

public class Download extends Activity {
/** Called when the activity is first created. */
private Button downloadTxtButton;
private Button downloadMp3Button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
downloadTxtButton = (Button)findViewById(R.id.downloadTxt);
downloadTxtButton.setOnClickListener(new DownloadTxtListener());
downloadMp3Button = (Button)findViewById(R.id.downloadMp3);
downloadMp3Button.setOnClickListener(new DownloadMp3Listener());
}

 

class DownloadTxtListener implements OnClickListener{

 

@Override
public void onClick(View v) {
HttpDownloader httpDownloader = new HttpDownloader();
String lrc = httpDownloader.download("http://127.0.0.1:8080/nation/111.txt");
System.out.println(lrc);
}

 

}
class DownloadMp3Listener implements OnClickListener{

 

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
HttpDownloader httpDownloader = new HttpDownloader();
// 這裡位址是用ipconfig查看自己的ip 原先一直用localhost 那樣是不行的
int result = httpDownloader.downFile("http://111.174.75.72:8080/Test/pic/baihe.mp3", "voa/", "a1.mp3");
System.out.println(result);
}

 

}

 

}



HttpDownloader 對下載操作===================================================

 

package mars.utils;

 

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.MalformedURLException;
import java.net.URL;



public class HttpDownloader {
private URL url = null;

 

/**
* 根據URL下載檔案,前提是這個檔當中的內容是文本,函數的返回值就是檔當中的內容
* 1.創建一個URL物件
* 2.通過URL物件,創建一個HttpURLConnection物件
* 3.得到InputStram
* 4.從InputStream當中讀取資料
* @param urlStr
* @return
*/
public String download(String urlStr) {
StringBuffer sb = new StringBuffer();
String line = null;
BufferedReader buffer = null;
try {
// 創建一個URL物件
url = new URL(urlStr);
// 創建一個Http連接
HttpURLConnection urlConn = (HttpURLConnection) url
.openConnection();
// 使用IO流讀取資料
buffer = new BufferedReader(new InputStreamReader(urlConn
.getInputStream()));
while ((line = buffer.readLine()) != null) {
sb.append(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
buffer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return sb.toString();
}

 

/**
* 返回整形 -1:下載檔案出錯
* 返回0:代表下載檔案成功
* 返回1:代表檔已經存在
*/
public int downFile(String urlStr, String path, String fileName) {
InputStream inputStream = null;
try {
FileUtils fileUtils = new FileUtils();

 

if (fileUtils.isFileExist(path + fileName)) {
return 1;
} else {
inputStream = getInputStreamFromUrl(urlStr);
File resultFile = fileUtils.write2SDFromInput(path,fileName, inputStream);
if (resultFile == null) {
return -1;
}
}
} catch (Exception e) {
e.printStackTrace();
return -1;
} finally {
try {
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return 0;
}

 

/**
* 根據URL得到輸入流
*
* @param urlStr
* @return
* @throws MalformedURLException
* @throws IOException
*/
public InputStream getInputStreamFromUrl(String urlStr)
throws MalformedURLException, IOException {
url = new URL(urlStr);
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
InputStream inputStream = urlConn.getInputStream();
return inputStream;
}
}




對檔案的操作=-===================
package mars.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.os.Environment;

public class FileUtils {
private String SDPATH;

public String getSDPATH() {
return SDPATH;
}
public FileUtils() {
//得到當前外部存放裝置的目錄
// /SDCARD
SDPATH = Environment.getExternalStorageDirectory() + "/";
}
/**
* 在SD卡上創建檔
*
* @throws IOException
*/
public File creatSDFile(String fileName) throws IOException {
File file = new File(SDPATH + fileName);
file.createNewFile();
return file;
}

/**
* 在SD卡上創建目錄
*
* @param dirName
*/
public File creatSDDir(String dirName) {
File dir = new File(SDPATH + dirName);
dir.mkdirs();
return dir;
}

/**
* 判斷SD卡上的資料夾是否存在
*/
public boolean isFileExist(String fileName){
File file = new File(SDPATH + fileName);
return file.exists();
}

/**
* 將一個InputStream裡面的資料寫入到SD卡中
*/
public File write2SDFromInput(String path,String fileName,InputStream input){
File file = null;
OutputStream output = null;
try{
creatSDDir(path);
file = creatSDFile(path + fileName);
output = new FileOutputStream(file);
byte buffer [] = new byte[4 * 1024];
while((input.read(buffer)) != -1){
output.write(buffer);
}
output.flush();
}
catch(Exception e){
e.printStackTrace();
}
finally{
try{
output.close();
}
catch(Exception e){
e.printStackTrace();
}
}
return file;
}

}
代码来自mars.droid  
arrow
arrow
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()