//判斷外部儲存器的狀態 String state = Environment.getExternalStorageState(); if (state.equals(Environment.MEDIA_MOUNTED)) { String folderPath = String.format("%s/MyApp", Environment.getExternalStorageDirectory().getPath()); String filePath = folderPath + "/NotesRecord.xml"; File file = new File(folderPath); if (!file.exists()) { file.mkdir(); file = new File(filePath); file.createNewFile(); } else { //Add code here }
並在Manifest文件中添加如下權限java
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>