Android-20170506

xiaoxiao2021-02-28  91

汗颜,快一个月没写了。。。。

Saving Data

1.保存相对较小的键值集合,使用SharedPreferences

 使用getSharedPreferences()(可从应用中任何context调用此方法)或者getPreferences()(检索属于该Activity的)

Context context = getActivity(); SharedPreferences sharedPref = context.getSharedPreferences(         getString(R.string.preference_file_key), Context.MODE_PRIVATE); 或者

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); 写入

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPref.edit(); editor.putInt(getString(R.string.saved_high_score), newHighScore); editor.commit(); 读取 SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); int defaultValue = getResources().getInteger(R.string.saved_high_score_default); long highScore = sharedPref.getInt(getString(R.string.saved_high_score), defaultValue); 2.File保存文件 File对象适合顺序不跳过的读取或写入大量数据 (1)内部存储与外部存储 内部存储:非易失性内存 特点:始终可用,只有本应用能访问,用户卸载应用时会移除 外部存储:如SD卡 特点:并非始终可用,所有应用都能访问,卸载应用时,使用getExternalFilesDir()保存在目录中时,系统才会移除 (2)获取外部存储权限 <manifest ...>     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />     ... </manifest> 只读 <manifest ...>     <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />     ... </manifest> 只写 WRITE_EXTERNAL_STORAGE 无需任何权限,即可在内部存储中保存文件,始终拥有读写权限 (3)将文件保存在内部存储中 getFilesDir()    返回应用的内部目录File getCacheDir()  返回应用的临时缓存文件File 新建文件 File file = new File(context.getFilesDir(), filename); 向文件写入文本 String filename = "myfile"; String string = "Hello world!"; FileOutputStream outputStream; try {   outputStream = openFileOutput(filename, Context.MODE_PRIVATE);   outputStream.write(string.getBytes());   outputStream.close(); } catch (Exception e) {   e.printStackTrace(); } 缓存文件 public File getTempFile(Context context, String url) {     File file;     try {         String fileName = Uri.parse(url).getLastPathSegment();         file = File.createTempFile(fileName, null, context.getCacheDir());     } catch (IOException e) {         // Error while creating file     }     return file; } (4)将文件保存在外部存储中 getExternalStorageState()查询外部存储容量,返回MEDIA_MOUNTED即可使用 以下两个方法是查询是否可用的两个例子 /* Checks if external storage is available for read and write */ public boolean isExternalStorageWritable() {     String state = Environment.getExternalStorageState();     if (Environment.MEDIA_MOUNTED.equals(state)) {         return true;     }     return false; } /* Checks if external storage is available to at least read */ public boolean isExternalStorageReadable() {     String state = Environment.getExternalStorageState();     if (Environment.MEDIA_MOUNTED.equals(state) ||         Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {         return true;     }     return false; } 尽管外部存储可被其他App使用,但是依然可以保存两类文件 公共文件: 即使删除App,用户仍可使用,如照片 getExternalStoragePublicDirectory() 私有文件: 避免向其他App泄露资源 (5)查询可用空间 getFreeSpace() getTotalSpace() (6)删除文件 myFile.delete(); myContext.deleteFile(fileName); 3.用SQL数据库保存数据 包在android.database.sqlite中提供 详见官网和自己买的那本书(太多了懒得写)

转载请注明原文地址: https://www.6miu.com/read-57788.html

最新回复(0)