W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
嘿嘿,看到這個題目,相信部分讀者會問,你前面的Fragment寫完了嗎?嗯,沒寫完,因為想例子,需要 一點時間,為了提高效率,所以決定像多線程一樣,并發(fā)的來寫教程,這樣可能可以加快寫教程的進度, 到現(xiàn)在為止,剛好寫了60篇,離完成入門教程還很遠呢,而前面也說過,想在一個半到兩個月之內(nèi)完成 這套教程,今天已經(jīng)9.1號了,要加吧勁~好的,廢話就這么多,本節(jié)給大家介紹的是Android數(shù)據(jù)存儲與 訪問方式中的一個——文件存儲與讀寫,當然除了這種方式外,我們可以存到SharedPreference,數(shù)據(jù)庫, 或者Application中,當然這些后面都會講,嗯,開始本節(jié)內(nèi)容~
學過Java的同學都知道,我們新建文件,然后就可以寫入數(shù)據(jù)了,但是Android卻不一樣,因為Android是 基于Linux的,我們在讀寫文件的時候,還需加上文件的操作模式,Android中的操作模式如下:
嘿嘿,果然讀寫SD卡成功~接下來我們來看下代碼是怎么寫的:
代碼實現(xiàn):
main_activity.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.jay.example.filedemo2.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="清輸入文件名" />
<EditText
android:id="@+id/edittitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="文件名" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="清輸入文件內(nèi)容" />
<EditText
android:id="@+id/editdetail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="文件內(nèi)容" />
<Button
android:id="@+id/btnsave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="保存到SD卡" />
<Button
android:id="@+id/btnclean"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="清空" />
<Button
android:id="@+id/btnread"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="讀取sd卡中的文件" />
</LinearLayout>
接著我們來寫一個SD操作類: SDFileHelper.java
/**
* Created by Jay on 2015/9/1 0001.
*/
public class SDFileHelper {
private Context context;
public SDFileHelper() {
}
public SDFileHelper(Context context) {
super();
this.context = context;
}
//往SD卡寫入文件的方法
public void savaFileToSD(String filename, String filecontent) throws Exception {
//如果手機已插入sd卡,且app具有讀寫sd卡的權限
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
filename = Environment.getExternalStorageDirectory().getCanonicalPath() + "/" + filename;
//這里就不要用openFileOutput了,那個是往手機內(nèi)存中寫數(shù)據(jù)的
FileOutputStream output = new FileOutputStream(filename);
output.write(filecontent.getBytes());
//將String字符串以字節(jié)流的形式寫入到輸出流中
output.close();
//關閉輸出流
} else Toast.makeText(context, "SD卡不存在或者不可讀寫", Toast.LENGTH_SHORT).show();
}
//讀取SD卡中文件的方法
//定義讀取文件的方法:
public String readFromSD(String filename) throws IOException {
StringBuilder sb = new StringBuilder("");
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
filename = Environment.getExternalStorageDirectory().getCanonicalPath() + "/" + filename;
//打開文件輸入流
FileInputStream input = new FileInputStream(filename);
byte[] temp = new byte[1024];
int len = 0;
//讀取文件內(nèi)容:
while ((len = input.read(temp)) > 0) {
sb.append(new String(temp, 0, len));
}
//關閉輸入流
input.close();
}
return sb.toString();
}
}
接著MainActivity.java實現(xiàn)相關邏輯:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private EditText editname;
private EditText editdetail;
private Button btnsave;
private Button btnclean;
private Button btnread;
private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = getApplicationContext();
bindViews();
}
private void bindViews() {
editname = (EditText) findViewById(R.id.edittitle);
editdetail = (EditText) findViewById(R.id.editdetail);
btnsave = (Button) findViewById(R.id.btnsave);
btnclean = (Button) findViewById(R.id.btnclean);
btnread = (Button) findViewById(R.id.btnread);
btnsave.setOnClickListener(this);
btnclean.setOnClickListener(this);
btnread.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btnclean:
editdetail.setText("");
editname.setText("");
break;
case R.id.btnsave:
String filename = editname.getText().toString();
String filedetail = editdetail.getText().toString();
SDFileHelper sdHelper = new SDFileHelper(mContext);
try
{
sdHelper.savaFileToSD(filename, filedetail);
Toast.makeText(getApplicationContext(), "數(shù)據(jù)寫入成功", Toast.LENGTH_SHORT).show();
}
catch(Exception e){
e.printStackTrace();
Toast.makeText(getApplicationContext(), "數(shù)據(jù)寫入失敗", Toast.LENGTH_SHORT).show();
}
break;
case R.id.btnread:
String detail = "";
SDFileHelper sdHelper2 = new SDFileHelper(mContext);
try
{
String filename2 = editname.getText().toString();
detail = sdHelper2.readFromSD(filename2);
}
catch(IOException e){e.printStackTrace();}
Toast.makeText(getApplicationContext(), detail, Toast.LENGTH_SHORT).show();
break;
}
}
}
最后別忘記在AndroidManifest.xml寫上讀寫SD卡的權限哦!
<!-- 在SDCard中創(chuàng)建與刪除文件權限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<!-- 往SDCard寫入數(shù)據(jù)權限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
如果是真機調(diào)試的話通常都是可以的,對于原生虛擬機的話就問題多多了,再我們前面使用 Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)可能 一直返回的是false,就是SD卡不存在,這個是主要的問題,現(xiàn)在新版本的SDK都會在 創(chuàng)建AVD的時候會同時申請一塊SD卡的存儲區(qū)域的
對于舊版本的sdk或者其他原因可能需要手動關聯(lián)下sd卡,設置如下:
①找到創(chuàng)建好的avd的鏡像的路徑:
點擊打開avd界面,點擊detail,查看avd鏡像的目錄下
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: