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); //當輸入輸出都指定字符集編碼的時候,就不會出現(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.得到當前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)文章
Android Zxing 轉(zhuǎn)換豎屏掃描且提高識別率的方法
本篇文章主要介紹了Android Zxing 轉(zhuǎn)換豎屏掃描且提高識別率的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05刷新Activity中的scrollview示例(局部ui刷新)
代碼很簡單,但是很實用,適合在一個Activity中要刷新局部的UI,比如在掃描一維碼的時候,要把每次掃描的結(jié)果都顯示在界面上2014-01-01android 對話框彈出位置和透明度的設(shè)置具體實現(xiàn)方法
在android中我們經(jīng)常會用AlertDialog來顯示對話框。通過這個對話框是顯示在屏幕中心的。但在某些程序中,要求對話框可以顯示在不同的位置。2013-07-07Android筆記之:onConfigurationChanged詳解
本篇是對Android中onConfigurationChanged的使用進行了詳細的分析介紹。需要的朋友參考下2013-05-05Android的TextView與Html相結(jié)合的具體方法
Android的TextView與Html相結(jié)合的具體方法,需要的朋友可以參考一下2013-06-06談?wù)凙ndroid開發(fā)之RecyclerView的使用全解
這篇文章主要介紹了談?wù)凙ndroid開發(fā)之RecyclerView的使用全解,非常具有實用價值,需要的朋友可以參考下。2016-12-12Android中關(guān)于Notification及NotificationManger的詳解
本篇文章小編為大家介紹,Android中關(guān)于Notification及NotificationManger的詳解。需要的朋友參考下2013-04-04