Android项目上难免要使用sd卡存储,前段时间用过之后,现在想起来总结一下,顺便以后方便查阅。也写一个小demo。
源码地址:http://download.csdn.net/detail/qq_16064871/8548857
下面开始贴代码,代码上有注释。
一、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 block e.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; @Override protected 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="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <ScrollView android:id="@+id/scrollView_text" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="输入数据:" /> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" > <requestFocus /> </EditText> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="60dp" > <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="打开数据:" /> <TextView android:id="@+id/textView3" android:layout_width="match_parent" android:layout_height="50dp" android:text="TextView" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="存储位置以及文件名:" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:layout_marginTop="5dp" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dip" android:onClick="SavePraseData" android:text="写入sd卡" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dip" android:onClick="OpenPraseData" android:text="打开文件" /> </LinearLayout> </LinearLayout> </ScrollView> </LinearLayout>
四、记得在AndroidManifest加入权限。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
就这些了,一定记得要加入权限。新手不断成长中,欢迎交流。
项目资源下载:http://download.csdn.net/detail/qq_16064871/8548857
转载请注明出处: http://blog.csdn.net/qq_16064871
时间: 2024-11-05 17:25:08