Android應用開發(fā)中數(shù)據(jù)的保存方式總結(jié)
一、保存文件到手機內(nèi)存
/** * 保存數(shù)據(jù)到手機rom的文件里面. * @param context 應用程序的上下文 提供環(huán)境 * @param name 用戶名 * @param password 密碼 * @throws Exception */ public static void saveToRom(Context context, String name , String password) throws Exception{ //File file = new File("/data/data/com.itheima.login/files/info.txt"); File file = new File(context.getFilesDir(),"info.txt");//該文件在data下的files文件夾下getCacheDir()在cache文件夾下 文件大小不要超過1Mb FileOutputStream fos = new FileOutputStream(file); String txt = name+":"+password; fos.write(txt.getBytes()); fos.flush(); fos.close(); }
/** * 獲取保存的數(shù)據(jù) * @param context * @return */ public static Map<String,String> getUserInfo(Context context) { File file = new File(context.getFilesDir(),"info.txt"); try { FileInputStream fis = new FileInputStream(file); //也可直接讀取文件String result = StreamTools.readFromStream(fis); BufferedReader br = new BufferedReader(new InputStreamReader(fis)); String str = br.readLine(); String[] infos = str.split(":"); Map<String,String> map = new HashMap<String, String>(); map.put("username", infos[0]); map.put("password", infos[1]); return map; } catch(Exception e) { e.printStackTrace(); return null; } } //最后可以直接調(diào)用上面的方法讀取信息 Map<String, String> map = getUserInfo(this); If(map!=null){ Textview.setText(map.get(“username”)); }
二、保存文件到SD卡
獲取手機sd空間的大?。?/p>
File path = Environment.getExternalStorageDirectory(); StatFs stat = new StatFs(path.getPath()); long blockSize = stat.getBlockSize(); long totalBlocks = stat.getBlockCount(); long availableBlocks = stat.getAvailableBlocks(); long totalSize = blockSize*totalBlocks; long availSize = blockSize * availableBlocks; String totalStr = Formatter.formatFileSize(this,totalSize); String availStr = Formatter.formatFileSize(this, availSize); tv.setText("總空間"+totalStr+"\n"+"可用空間"+availStr);
加入寫外部存儲的權(quán)限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> public static void save(String name ,String password) throws Exception{ if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ File file = new File(Environment.getExternalStorageDirectory(),"info.txt"); //也可直接寫/sdcard/info.txt 先判斷sd卡是否存在 FileOutputStream fos = new FileOutputStream(file); String txt = name+":"+password; fos.write(txt.getBytes()); fos.flush(); fos.close(); // 使用RandomAccessFile像文件追加內(nèi)容FileOutputStream會把原有的文件內(nèi)容清空 //RandomAccessFile raf = new RandomAccessFile(file,"rw"); //raf.seek(file.length()); 將文件指針移動到最后 //raf.write(name.getBytes()+password.getBytes()); //raf.close(); } }
//讀取文件 加入讀取權(quán)限 public static String read(){ try { if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ File sdcardDir = Environment.getExternalStorageDirectory(); FileInputStream fis = new FileInputStream(sdcardDir.getCanonicalPath() + "info.txt"); BufferedReader br = new BufferedReader(new InputStreamReader(fis)); StringBuilder sb = new StringBuilder(""); String line = null; while ((line = br.readLine())!= null){ sb.append(line); } return sb.toString(); } } catch (Exception e) { e.printStackTrace(); } return null; }
三、Sharedpreferences的使用
SharedPreference是開發(fā)中常用的一種存儲方式,主要存儲一些系統(tǒng)不變的參數(shù)如是否是第一次進入應用程序等,通過鍵值對的方式進行存儲
可以存儲的類型:booleans, floats, ints, longs,strings.
getSharedPreferences() - 存儲多個參數(shù)
getPreferences() - 僅存儲一個參數(shù)并且不需要指定名字(key)
寫入的步驟:
SharedPreferences調(diào)用edit()得到一個Editor對象
使用 putBoolean() and putString()添加值
提交事務完成存儲
讀取時:只需要調(diào)用SharedPreferences的getBoolean() and getString()
下面是示例代碼:
public class MySharedPreference { private Context context; private SharedPreferences sp ; private Editor edit; public MySharedPreference(Context context){ this.context = context; } public boolean saveMessage(String name,String pwd){ boolean flag = false; sp = context.getSharedPreferences("userInfo",Context.MODE_PRIVATE); //MODE定義了訪問的權(quán)限現(xiàn)在是本應用可以訪問 edit = sp.edit(); edit.putString("name", name); edit.putString("pwd", pwd); flag = edit.commit();//提交事務將數(shù)據(jù)持久化到存儲器中 return flag; } public Map<String,Object> getMessage(){ Map<String,Object> map = new HashMap<String, Object>(); sp = context.getSharedPreferences("userInfo", Context.MODE_PRIVATE); String name = sp.getString("name", ""); String pwd = sp.getString("pwd", ""); map.put("name", name); map.put("pwd",pwd); return map; } }
- Android實現(xiàn)EditText內(nèi)容保存為Bitmap的方法
- Android實現(xiàn)內(nèi)存中數(shù)據(jù)保存到sdcard的方法
- Android開發(fā)之完成登陸界面的數(shù)據(jù)保存回顯操作實例
- Android實現(xiàn)截屏并保存操作功能
- 基于Android如何實現(xiàn)將數(shù)據(jù)庫保存到SD卡
- Android編程實現(xiàn)手繪及保存為圖片的方法(附demo源碼下載)
- android中把文件保存到sdcard代碼實例
- android保存Bitmap圖片到指定文件夾示例
- Android截屏保存png圖片的實例代碼
- android創(chuàng)建數(shù)據(jù)庫(SQLite)保存圖片示例
相關(guān)文章
Android編程實現(xiàn)調(diào)用系統(tǒng)圖庫與裁剪圖片功能
這篇文章主要介紹了Android編程實現(xiàn)調(diào)用系統(tǒng)圖庫與裁剪圖片功能,結(jié)合實例形式分析了Android針對圖形的旋轉(zhuǎn)與剪切等具體操作技巧,需要的朋友可以參考下2017-01-01Android 加載大圖及多圖避免程序出現(xiàn)OOM(OutOfMemory)異常
這篇文章主要介紹了Android 加載大圖及多圖避免程序出現(xiàn)OOM(OutOfMemory)異常的相關(guān)資料,需要的朋友可以參考下2017-03-03Kotlin + Flow 實現(xiàn)Android 應用初始化任務啟動庫
這篇文章主要介紹了Kotlin + Flow 實現(xiàn)Android 應用初始化任務啟動庫的方法,幫助大家更好的理解和學習使用Android,感興趣的朋友可以了解下2021-03-03Android?妙用TextView實現(xiàn)左邊文字,右邊圖片
這篇文章主要介紹了Android?妙用TextView實現(xiàn)左邊文字,右邊圖片的相關(guān)資料,需要的朋友可以參考下2023-07-07項目發(fā)布Debug和Release版的區(qū)別詳解
這篇文章主要為大家詳細介紹了項目發(fā)布Debug和Release版的區(qū)別,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-10-10Android獲取常用輔助方法(獲取屏幕高度、寬度、密度、通知欄高度、截圖)
我們需要獲取Android手機或Pad的屏幕的物理尺寸,以便于界面的設計或是其他功能的實現(xiàn)。下面就分享一下Android中常用的一些輔助方法2016-02-02