編輯:關於 Android 開發
本文示範如何在 Android 中把資料寫入外部儲存卡(SD 卡),並讀取回來顯示。
布局檔:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/save" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="保存數據(File)" /> <Button android:id="@+id/read" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="讀取數據(File)" /> </LinearLayout>
MainActivity.java:
package com.example.yanlei.wifi; import android.os.Bundle; import android.os.Environment; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.PrintStream; import java.util.Scanner; public class MainActivity extends AppCompatActivity { private Button btnSave=null; private Button btnRead=null; private File file=null; private static final String FILENAME="data.txt"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnSave=(Button)super.findViewById(R.id.save); btnRead=(Button)super.findViewById(R.id.read); btnSave.setOnClickListener(new OnClickListener(){ public void onClick(View v) { PrintStream ps=null; // 判斷外部存儲卡是否存在 if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ Toast.makeText(getApplicationContext(), "讀取失敗,SD存儲卡不存在!", Toast.LENGTH_LONG).show(); return; } // 初始化 File String path=Environment.getExternalStorageDirectory().toString() +File.separator +"genwoxue" +File.separator +FILENAME; file=new File(path); // 如果當前文件的父文件夾不存在,則創建 if(!file.getParentFile().exists()) file.getParentFile().mkdirs(); // 寫文件 try { ps = new PrintStream(new FileOutputStream(file)); ps.println("跟我學網址:www.genwoxue.com"); ps.println(""); ps.println("電子郵件:hello@genwoxue.com"); } catch (FileNotFoundException e) { e.printStackTrace(); }finally{ ps.close(); } Toast.makeText(getApplicationContext(), "保存成功!", Toast.LENGTH_LONG).show(); } }); btnRead.setOnClickListener(new OnClickListener(){ public void onClick(View v) { StringBuffer info=new StringBuffer(); // 判斷外部存儲卡是否存在 if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ Toast.makeText(getApplicationContext(), "讀取失敗,SD存儲卡不存在!", Toast.LENGTH_LONG).show(); return; } // 初始化 File String path=Environment.getExternalStorageDirectory().toString() +File.separator +"genwoxue" +File.separator +FILENAME; file=new File(path); if(!file.exists()){ Toast.makeText(getApplicationContext(), "文件不存在!", Toast.LENGTH_LONG).show(); return; } // 讀取文件內容 Scanner scan=null; try { scan=new Scanner(new FileInputStream(file)); while(scan.hasNext()){ info.append(scan.next()).append("☆☆☆\n"); } Toast.makeText(getApplicationContext(), info.toString(), Toast.LENGTH_LONG).show(); } catch (FileNotFoundException e) { e.printStackTrace(); }finally{ scan.close(); } } }); } }
權限配置:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="https://schemas.android.com/apk/res/android" package="com.example.yanlei.wifi" > <!-- 在 SDCard 中創建與刪除文件權限 --> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <!-- 往 SDCard 寫入數據權限 --> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
其中讀寫外部儲存卡的關鍵權限為:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
寫入前記得先判斷外部儲存卡是否已掛載,並確保父資料夾存在,這樣就能順利把資料存到 SD 卡並讀回來。
Android Studio 建立 AVD 與 HAXM 問題排除
Android Studio 建立 AVD 與 HAXM 問題排除 Android Studio 是 Android 開發常用 IDE,而模擬器 AVD 則是測試 App
Android 廣播與通知實作:靜態註冊、動態註冊與 Notification
Android 廣播與通知實作:靜態註冊、動態註冊與 Notification BroadcastReceiver 是 Android 的系統級監聽元件,可用來接收系統或
開箱推薦:一個基於百度數據的新聞客戶端完整源碼
開箱推薦:一個基於百度數據的新聞客戶端完整源碼 想找一個資料來源現成、結構又看得懂的新聞客戶端範例,這個專案值得收藏。它直接採用百度的新聞數據介面,不用自己搭後端就能跑起
状态栏一体化实现及其引发的软键盘自适应问题
状态栏一体化实现及其引发的软键盘自适应问题 因项目需求,我才开始了解状态栏一体化的问题。作为一个 Android 新手,之前从未接触过,第一反应是上网搜索。网络确实帮了大