Android sd存储总结

Android项目上难免要使用sd卡存储,前段时间用过之后,现在想起来总结一下,,顺便以后方便查阅。也写一个小demo。

源码地址:

下面开始贴代码,代码上有注释。

一、DataManage类

package com.example.storagemanagedemo;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import android.os.Environment;import android.util.Log;public class DataManage {// 打开txt文件public String OpenTxtFile(String strPathFileName) {String strTxtData = "";// 获取sd卡存储路径根目录File pathFile = Environment.getExternalStorageDirectory();// 判断sd卡是否存在boolean sdCardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);if (sdCardExist) {String strpathFileName = pathFile.toString() + strPathFileName;// 判断是否存在这个路径是否存在这个文件File absolutPathFileName = new File(strpathFileName);if (!absolutPathFileName.exists()) {} else {strTxtData = ReadTxtDataSDFile(strpathFileName);}}return strTxtData;}// 保存txt格式文件public boolean SaveTxtFile(String strPath, String strFileName,String strSaveTxtData) {// 判断sd卡是否存在boolean sdCardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);if (sdCardExist) {// 获取sd卡存储路径根目录File pathFile = Environment.getExternalStorageDirectory();// 按照SD卡指定的路径创建文件夹File absolutPathFile = new File(pathFile.toString() + strPath);if (!absolutPathFile.exists()) {absolutPathFile.mkdirs();}// 在指定的文件夹中创建文件String strPathFileName = pathFile.toString() + strPath+ strFileName;File nameFile = new File(strPathFileName);if (!nameFile.exists()) {try {nameFile.createNewFile();} catch (IOException e) {e.printStackTrace();Log.i("Show", e.toString());}}// 调用函数向文件写入数据try {WiteTxtDataSDFile(strPathFileName, strSaveTxtData);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}return false;}private String ReadTxtDataSDFile(String strpathFileName) {String strTxtData = "";try {FileInputStream fileInputStream = new FileInputStream(strpathFileName);int length = fileInputStream.available();byte[] buffer = new byte[length];fileInputStream.read(buffer);strTxtData = new String(buffer);fileInputStream.close();} catch (Exception e) {e.printStackTrace();}return strTxtData;}// 向文件写入数据private void WiteTxtDataSDFile(String strPathFileName, String strSaveTxtData)throws IOException {File writefile = new File(strPathFileName);FileOutputStream fileOutputStream = new FileOutputStream(writefile);byte[] buffer = strSaveTxtData.getBytes();try {fileOutputStream.write(buffer);fileOutputStream.close();} catch (IOException e) {e.printStackTrace();}}}

二、MainActivity类

package com.example.storagemanagedemo;import java.io.File;import android.os.Bundle;import android.os.Environment;import android.view.View;import android.widget.EditText;import android.widget.TextView;import android.app.Activity;public class MainActivity extends Activity {private DataManage mDataManage = new DataManage();private TextView showTextView;private TextView storageTextView;private EditText inputEditText;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);showTextView = (TextView) findViewById(R.id.textView3);storageTextView = (TextView) findViewById(R.id.textView1);inputEditText = (EditText) findViewById(R.id.editText1);File pathFile = Environment.getExternalStorageDirectory();String strpath = "存储位置以及文件名:" + pathFile.toString() + "/txt/result.txt";storageTextView.setText(strpath);}// 按键保存文件public void SavePraseData(View v) {mDataManage.SaveTxtFile("/txt", "/result.txt", inputEditText.getText().toString());}// 按键打开文件public void OpenPraseData(View v) {String strShowDataString = "";strShowDataString = mDataManage.OpenTxtFile("/txt/result.txt");showTextView.setText(strShowDataString);}}

三、xml

<LinearLayout xmlns:android=""xmlns:tools=""android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity" ><ScrollViewandroid:id="@+id/scrollView_text"android:layout_width="match_parent"android:layout_height="match_parent" ><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content" ><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="输入数据:" /><EditTextandroid:id="@+id/editText1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:ems="10" ><requestFocus /></EditText></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="60dp" ><TextViewandroid:id="@+id/textView2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="打开数据:" /><TextViewandroid:id="@+id/textView3"android:layout_width="match_parent"android:layout_height="50dp"android:text="TextView" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content" ><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="存储位置以及文件名:" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="50dp"android:layout_marginTop="5dp" ><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="5dip"android:onClick="SavePraseData"android:text="写入sd卡" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="5dip"android:onClick="OpenPraseData"android:text="打开文件" /></LinearLayout></LinearLayout></ScrollView></LinearLayout>才能做到人在旅途,感悟人生,享受人生。

Android sd存储总结

相关文章:

你感兴趣的文章:

标签云: