[英]Code not writing database to sdcard
我正在使用以下代碼將數據庫副本導出到sdcard。
public class AgUtility extends AgActivity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.utility);
try {
backupDatabase(getBaseContext());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void backupDatabase(Context context) throws IOException {
// Open your local db as the input stream
String inFileName = "data/data/com.agmanagement.todaysstudent/databases/todaysstudent.db";
Toast.makeText(context, "FileName Is "+ inFileName, Toast.LENGTH_LONG).show();
Log.i("The File In Is ", inFileName);
File dbFile = new File(inFileName);
FileInputStream fis = new FileInputStream(dbFile);
File outputDirectory = new File(
Environment.getExternalStorageDirectory() + "/student/");
outputDirectory.mkdir();
Log.d("MAKE DIR", dbFile.mkdir() + "");
String backupFileName = "/TodaysStudentTest.db3";
String outFileName = outputDirectory + backupFileName;
Toast.makeText(context, "Database backup names is " + outFileName , Toast.LENGTH_LONG)
.show();
// Open the empty db as the output stream
OutputStream output = new FileOutputStream(outFileName);
// transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
// Close the streams
output.flush();
output.close();
fis.close();
Toast.makeText(context, "Database backup complete", Toast.LENGTH_LONG)
.show();
}
}
該代碼似乎正常運行,因為我沒有任何錯誤,第一個Toast顯示了正確的數據庫名稱,第二個Toast顯示了輸出目錄應該是mnt / sdcard / student,第三個顯示了最終目標應該是mnt / sdcard / student / TodaysStudentTest.db3
在Toast消失之后,什么都沒有,最終的Toast永遠不會出現。
我的清單上有
我正在三星平板電腦而不是模擬器上進行測試,我也在DroidX上運行了相同的結果,沒有錯誤,但是沒有創建文件夾。
關於我在做什么錯的任何想法嗎? TIA
我正在使用的權限是
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.premission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.SET_DEBUG_APP" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.READ_CALENDAR"/>
<uses-permission android:name="android.permission.WRITE_CALENDAR"/>
在模擬器中運行時,我得到相同的結果-用DDMS觀看-Logcat顯示MAKE DIR失敗。
我已經為此測試了狀態
if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
mExternalStorageAvailable = mExternalStorageWriteable = true;
Toast.makeText(getBaseContext(), "We Can Read And Write To The SDCARD", Toast.LENGTH_LONG).show();
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// We can only read the media
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
Toast.makeText(getBaseContext(), "We Can Read The SDCARD", Toast.LENGTH_LONG).show();
} else {
// Something else is wrong. It may be one of many other states, but all we need
// to know is we can neither read nor write
mExternalStorageAvailable = mExternalStorageWriteable = false;
Toast.makeText(getBaseContext(), "We Can't read or write", Toast.LENGTH_LONG).show();
}
它表明我應該能夠讀寫,所以我的寫作方式有問題。 我也將此添加到文本
boolean success = false;
if(!outputDirectory.exists()){
Toast.makeText(getBaseContext(), "Folder Doesn't Exist ", Toast.LENGTH_LONG)
.show();
success = outputDirectory.mkdirs();
}
if (!success){
Toast.makeText(getBaseContext(), "Folder Not Created ", Toast.LENGTH_LONG)
.show();
}
else{
Toast.makeText(getBaseContext(), "Folder Created ", Toast.LENGTH_LONG)
.show();
}
結果是文件夾不存在,然后mkdirs()失敗。
在嘗試寫入文件之前,嘗試手動創建文件。
改寫
這是一種不使用SQL本身或循環緩沖區而處理數據庫文件的不同方法。
注意:這實際上並沒有復制到sdcard,備份存儲在原始數據庫文件夾中(我喜歡這樣做,因為您不需要 WRITE_EXTERNAL_STORAGE權限)。
public class FileIO extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DBHelper db = new DBHelper(this);
try {
copyFile();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
Log.i("Main", "Complete");
db.close();
finish();
}
}
public void copyFile() throws IOException {
File data = Environment.getDataDirectory();
String state = Environment.getExternalStorageState();
/* Create file first
FileOutputStream created = openFileOutput("copyFile.db", MODE_WORLD_READABLE);
created.close();
*/
String currentDBPath = "/data/<your_path>/databases/data.db";
String backupDBPath = "/data/<your_path>/databases/copyByFile.db";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(data, backupDBPath);
if (currentDB.exists()) {
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
else
Log.i("Main", "Current db does not exist");
}
}
使用mkdir()時,請確保已創建名為“ student”的文件夾。 它將通過抽象路徑名創建目錄。.因此,如果文件夾“ student”不存在,則不會創建新文件夾..或嘗試改用mkdirs()。 如有必要,它將創建父文件夾。
重要的是要記住檢查拼寫。 Uses-permission的拼寫與uses-premission錯了,我讀了很多遍代碼,然后按自己的意願閱讀了。 寶貴的教訓,走開休息一下。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.