編輯:關於android開發
SharedPreference
以xml的結構儲存簡單的數據,儲存在data/data/程序包名/shared_prefs文件夾中
使用方式
創建對象的方式有三種
Context 的 getSharedPreferences()
Activity 的 getPreferences()
PreferenceManager 的 getDefaultSharedpreferences()
獲取數據
sharedPrefs.getXXX()方法,如getInt(),getString()等
儲存數據 獲取Editor對象 sharedPrefs.edit();
存入數據 editor.putXXX()方法,如putInt()、putString()
提交要保存的數據 editor.commit()

我們通過路徑找文件 看一下運行結果 路徑我在上面已經提到了 我使用的是Genymotion測試
訪問手機內部存儲需要root權限才可以訪問









內部存儲
將數據保存在內存空間中,數據用戶不能輕易訪問的區域,訪問需要root權限。存儲在/data/data/程序包名/files文件夾下
使用方式
其使用還是要通過FileInputStream和FileOutputStream對文件File進行操作,只不過不是通過他們的構造方法來創建。
獲取FileInputStream
FileInputStream fis = openFileInput(); (Activity的方法)
FileOuputStream fos = openFileOutput(); (Activity的方法)
文件File將會自動創建


外部SDCard存儲
在操作sd卡的時候需要在清單文件中添加權限

將數據保存到SDCard卡中,任何程序都可以訪問,用戶也很容易查看、修改。
使用方式
通過FileInputStream和FileOutputStream對文件File進行操作。
SDCard操作類Environment 進行SDCard狀態獲取
Environment.getExternalStorageState(); 獲取擴展卡狀態
Environment.MEDIA_MOUNTED 安裝的並可讀寫
Environment.MEDIA_MOUNTED_READ_ONLY 安裝的但只讀
Environment.MEDIA_REMOVED 移除的
獲取常用文件夾路徑
Environment.getExternalStorageDirectory(); 獲取擴展卡根文件夾
這個路徑在sd卡下自己寫 找不到 只能輕輕說聲加油~
貼一個小demo
運行效果
在edittext 輸入文本點擊寫入按鈕 點擊讀取顯示到textview 重要的事情說三遍 加權限~加權限~加權限~

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.lesson15_data_storage.MainActivity">
<CheckBox
android:id="@+id/push"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="是否追加到原文件末尾"/>
<EditText
android:id="@+id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="顯示文本"/>
<Button
android:id="@+id/inner_storage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="保存文本到內部存儲中"
android:onClick="onClick"/>
<Button
android:id="@+id/inner_read"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="讀取內部存儲的文本到TextView"
android:onClick="onClick"/>
<Button
android:id="@+id/outter_read"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="讀取擴展卡的文本到textview"/>
<Button
android:id="@+id/outter_write"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="將文本寫入到擴展卡中"/>
</LinearLayout>
MainActivity
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private CheckBox cbPush;
private TextView textView;
private EditText edit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cbPush= (CheckBox) findViewById(R.id.push);
textView= (TextView) findViewById(R.id.text_view);
edit= (EditText) findViewById(R.id.edit);
getSharedPreferences();
}
/**
* 存儲一個CheckBox的選中狀態
*/
private void getSharedPreferences() {
//Context獲取 參數1文件名 參數2文件權限
SharedPreferences preferences=getSharedPreferences("setting", Context.MODE_PRIVATE);
//activity獲取 文件名是當前活動名
SharedPreferences preferences1=getPreferences(MODE_PRIVATE);
//獲取editor對象
SharedPreferences.Editor editor1=preferences1.edit();
//保存數據
editor1.putString("text","通過activity對象獲取");
//提交數據
editor1.commit();
//PreferenceManager獲取 文件名自動生成 訪問權限默認私有
PreferenceManager.getDefaultSharedPreferences(MainActivity.this)
.edit()
.putString("text","通過PreferenceManager獲取")
.commit();
//參數1 key 參數2 默認值 基本數據類型要求不可以為空 所以必須有默認值
boolean checked=preferences.getBoolean("cbPush",false);
cbPush.setChecked(checked);
cbPush.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//通過context獲取 參數1 存儲的文件名 參數2 數據的訪問權限
SharedPreferences sharedPreferences=getSharedPreferences("setting", Context.MODE_PRIVATE);
//獲取編輯器對象
SharedPreferences.Editor edit=sharedPreferences.edit();
//存入數據
edit.putBoolean("cbPush",isChecked);
//提交修改
edit.commit();
}
});
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.inner_storage:
write();//寫入到內部文件
Toast.makeText(MainActivity.this,"寫入成功",Toast.LENGTH_SHORT).show();
break;
case R.id.inner_read:
read();//讀取內部文件
Toast.makeText(MainActivity.this,"讀取成功",Toast.LENGTH_SHORT).show();
break;
case R.id.outter_read:
oread();//讀取sdcard文件
Toast.makeText(MainActivity.this,"讀取成功",Toast.LENGTH_SHORT).show();
break;
case R.id.outter_write:
owrite();//寫入sdcard文件
Toast.makeText(MainActivity.this,"寫入成功",Toast.LENGTH_SHORT).show();
break;
}
}
private void oread() {
String state=Environment.getExternalStorageState();
try {
//判斷是否讀寫 或只讀
if(state.equals(Environment.MEDIA_MOUNTED)||state.equals(Environment.MEDIA_MOUNTED_READ_ONLY)){
File sdcardRoot=Environment.getExternalStorageDirectory();
File read=new File(sdcardRoot,"my/text.txt");
if(read.exists()){
FileInputStream fis=new FileInputStream(read);
//字節轉字符會進行字節編碼的轉換 new InputStreamReader(fis,"utf-8") android默認utf-8
BufferedReader reader=new BufferedReader(new InputStreamReader(fis));
String str=reader.readLine();
textView.setText(str);
reader.close();
fis.close();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void owrite() {
try {
//獲取擴展卡狀態
String state= Environment.getExternalStorageState();
//是否可讀寫
if(state.equals(Environment.MEDIA_MOUNTED)){
//獲取擴展卡文件夾目錄
File sdcardRoot=Environment.getExternalStorageDirectory();
File save=new File(sdcardRoot,"my/text.txt");
//判斷文件是否存在
if (save.exists()){
}else{
//判斷父級文件夾是否存在
if(!save.getParentFile().exists()){
//創建文件夾
save.getParentFile().mkdirs();
}
//創建文件
save.createNewFile();
}
String text=edit.getText().toString();
//文件是否在原文件末尾追加 cbPush.isChecked() checkbox 是否選中
FileOutputStream fos=new FileOutputStream(save,cbPush.isChecked());
fos.write(text.getBytes("utf-8"));
fos.flush();
fos.close();
// //是否是文件夾
// save.isDirectory();
// //是否是文件
// save.isFile();
// //是否是隱藏文件 文件名以 .開頭的文件
// save.isHidden();
// //獲取所有子文件的文件名數組
// save.list();
// //獲取所有子文件的文件數組
// save.listFiles();
}
} catch (IOException e) {
e.printStackTrace();
}
}
private void read() {
try {
//讀取寫入的文件
FileInputStream fis=openFileInput("text.txt");
int len=-1;
byte [] b=new byte[1024];
ByteArrayOutputStream bos=new ByteArrayOutputStream();
while ((len=fis.read(b))!=-1){
bos.write(b,0,len);
}
textView.setText(bos.toString("utf-8"));
bos.close();
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void write() {
try {
//獲取輸入框內容
String text=edit.getText().toString();
//判斷是否為空
if(TextUtils.isEmpty(text))return;
//寫入方式 追加或覆蓋
int mode=cbPush.isChecked()?MODE_APPEND:MODE_PRIVATE;
//獲取FileOutputStream
FileOutputStream fos=openFileOutput("text.txt",mode);
//字符串轉換為byte 寫入文件
fos.write(text.getBytes("utf-8"));
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
perf profiling 分析程序性能
perf profiling 分析程序性能perf profiling 分析程序性能程序性能分析perf 有一個功能就是按一定頻率采集某一個程序的調用棧,然後對調用棧進行
Android提高21篇之七:XML解析與生成
本文使用SAX來解析XML,在Android裡面可以使用SAX和DOM,DOM需要把整個XML文件
Android中的普通對話框、單選對話框、多選對話框、帶Icon的對話框、以及自定義Adapter和自定義View對話框詳解
Android中的普通對話框、單選對話框、多選對話框、帶Icon的對話框、以及自定義Adapter和自定義View對話框詳解 對話框就是一個AlertDialog,但
Unity Shaders and Effects Cookbook (4-1)(4-2)靜態立方體貼圖的創建與使用
Unity Shaders and Effects Cookbook (4-1)(4-2)靜態立方體貼圖的創建與使用 開始學習第4章 - 著色器的反射 看