Android App將數(shù)據(jù)寫入內(nèi)部存儲和外部存儲的示例
File存儲(內(nèi)部存儲)
一旦程序在設(shè)備安裝后,data/data/包名/ 即為內(nèi)部存儲空間,對外保密。
Context提供了2個方法來打開輸入、輸出流
- FileInputStream openFileInput(String name)
- FileOutputStream openFileOutput(String name, int mode)
public class MainActivity extends Activity {
private TextView show;
private EditText et;
private String filename = "test";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
show = (TextView) findViewById(R.id.show);
et = (EditText) findViewById(R.id.et);
findViewById(R.id.write).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
FileOutputStream fos = openFileOutput(filename, Context.MODE_PRIVATE);
//FileOutputStream是字節(jié)流,如果是寫文本的話,需要進一步把FileOutputStream包裝 UTF-8是編碼
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
//寫
osw.write(et.getText().toString());
osw.flush();
fos.flush();
osw.close();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
findViewById(R.id.read).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
FileInputStream fis = openFileInput(filename);
//當(dāng)輸入輸出都指定字符集編碼的時候,就不會出現(xiàn)亂碼的情況
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
//獲取文件的可用長度,構(gòu)建一個字符數(shù)組
char[] input = new char[fis.available()];
isr.read(input);
isr.close();
fis.close();
String readed = new String(input);
show.setText(readed);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
data/data/packagename/files/test就是我們寫入的文件。
SD存儲(外部存儲)
mnt/sdcard 目錄就是SD卡的掛載點(只是一個指向)。
storage/sdcard: 真正的SD卡操作目錄。
一、文件下載
Android開發(fā)中,有時需要從網(wǎng)上下載一些資源以供用戶使用,Android API中已經(jīng)提供了很多直接可以用的類供大家使用,一般文件下載需要通過三個步驟:
1.創(chuàng)建一個HttpURLConnection對象
// 創(chuàng)建一個URL對象,該對象包含一個IP地址,urlStr指的是網(wǎng)絡(luò)IP地址 url = new URL(urlStr); // 通過URL對象,來創(chuàng)建一個HttpURLConnection對象 HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
2.獲得一個InputStream對象
InputStream input = urlConn.getInputStream();
3.設(shè)置訪問網(wǎng)絡(luò)的權(quán)限
//在AndroidManifest.xml配置文件中加入權(quán)限信息 <uses-permission android:name="android.permission.INTERNET"/>
二、訪問并寫入SD卡
1.判斷手機上是否插入SD卡,且應(yīng)用程序具有讀寫權(quán)限
Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
2.得到當(dāng)前SD卡的目錄
Environment.getExternalStorageDirectory();
3.在訪問SD卡前還必須在配置文件中設(shè)置權(quán)限,這樣才可以最SD卡進行存取操作
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
以下是一個對SD操作經(jīng)常用到的封裝類,以后如果需要對SD卡操作,直接可以拿過來用
public class FileUtils {
private String SDPATH;
public String getSDPATH(){
return SDPATH;
}
//構(gòu)造函數(shù),得到SD卡的目錄,這行函數(shù)得到的目錄名其實是叫"/SDCARD"
public FileUtils() {
SDPATH = Environment.getExternalStorageDirectory() +"/";
}
//在SD卡上創(chuàng)建文件
public File createSDFile(String fileName) throws IOException{
File file = new File(SDPATH + fileName);
file.createNewFile();
return file;
}
//在SD卡上創(chuàng)建目錄
public File createSDDir(String dirName){
File dir = new File(SDPATH + dirName);
dir.mkdir();
return dir;
}
//判斷SD卡上的文件夾是否存在
public boolean isFileExist(String fileName){
File file = new File(SDPATH + fileName);
return file.exists();
}
//將一個InputStream里面的數(shù)據(jù)寫入到SD卡中
//將input寫到path這個目錄中的fileName文件上
public File write2SDFromInput(String path, String fileName, InputStream input){
File file = null;
OutputStream output = null;
try{
createSDDir(path);
file = createSDFile(path + fileName);
//FileInputStream是讀取數(shù)據(jù),F(xiàn)ileOutputStream是寫入數(shù)據(jù),寫入到file這個文件上去
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;
}
}
相關(guān)文章
詳解關(guān)于AndroidQ獲取不到imsi解決方案
這篇文章主要介紹了詳解關(guān)于AndroidQ獲取不到imsi解決方案,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
Android實現(xiàn)四級聯(lián)動地址選擇器
這篇文章主要為大家詳細介紹了Android實現(xiàn)四級聯(lián)動地址選擇器,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-10-10
Android中使用的定時針(刷新頁面請求服務(wù)器)詳解
這篇文章主要介紹了Android中使用的定時針(刷新頁面請求服務(wù)器)詳解的相關(guān)資料,需要的朋友可以參考下2016-12-12
分享安裝Android Studio3.6的經(jīng)驗教訓(xùn)
這篇文章主要介紹了Android Studio3.6的安裝錯誤問題及解決方法,非常值得大家參考,現(xiàn)把整個過程分享到腳本之家平臺,需要的朋友參考下吧2020-02-02
Android:Field can be converted to a local varible.的解決辦法
這篇文章主要介紹了Android:Field can be converted to a local varible.的解決辦法的相關(guān)資料,希望通過本文能幫助到大家,讓大家遇到這樣的問題輕松解決,需要的朋友可以參考下2017-10-10
利用Kotlin的方式如何處理網(wǎng)絡(luò)異常詳解
這篇文章主要 給大家介紹了關(guān)于利用Kotlin的方式如何處理網(wǎng)絡(luò)異常的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07

