[英]Android error: No such file or directory?
我正在嘗試通過我的 android 應用程序創建一個數據庫,該應用程序正在模擬器上工作。
但是,由於我無法直接在我的三星設備上查看數據庫(它沒有扎根),我正在嘗試將數據庫文件傳輸到 SD 卡。
該文件沒有成功傳輸到 SD 卡,當我嘗試在 eclipse 中打開它時它不存在。
我在 Logcat 中收到以下錯誤:
07-15 14:31:07.035: E/mypck(17369): /data/com.example.multapply/databases/MultapplyDatabase.db: open failed: ENOENT (No such file or directory)
07-15 14:31:07.035: E/mypck(17369): java.io.FileNotFoundException: /data/com.example.multapply/databases/MultapplyDatabase.db: open failed: ENOENT (No such file or directory)
07-15 14:31:07.035: E/mypck(17369): at libcore.io.IoBridge.open(IoBridge.java:409)
07-15 14:31:07.035: E/mypck(17369): at java.io.FileInputStream.<init>(FileInputStream.java:78)
07-15 14:31:07.035: E/mypck(17369): at com.example.multapply.ExportDatabaseFileTask.copyFile(ExportDatabaseFileTask.java:71)
07-15 14:31:07.035: E/mypck(17369): at com.example.multapply.ExportDatabaseFileTask.doInBackground(ExportDatabaseFileTask.java:49)
07-15 14:31:07.035: E/mypck(17369): at com.example.multapply.ExportDatabaseFileTask.doInBackground(ExportDatabaseFileTask.java:1)
07-15 14:31:07.035: E/mypck(17369): at android.os.AsyncTask$2.call(AsyncTask.java:288)
07-15 14:31:07.035: E/mypck(17369): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
07-15 14:31:07.035: E/mypck(17369): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
07-15 14:31:07.035: E/mypck(17369): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
07-15 14:31:07.035: E/mypck(17369): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
07-15 14:31:07.035: E/mypck(17369): at java.lang.Thread.run(Thread.java:841)
07-15 14:31:07.035: E/mypck(17369): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
07-15 14:31:07.035: E/mypck(17369): at libcore.io.Posix.open(Native Method)
07-15 14:31:07.035: E/mypck(17369): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
07-15 14:31:07.035: E/mypck(17369): at libcore.io.IoBridge.open(IoBridge.java:393)
07-15 14:31:07.035: E/mypck(17369): ... 10 more
相關代碼:
Class relating to exporting the file:
public class ExportDatabaseFileTask extends AsyncTask<String, Void, Boolean> {
//Default constructor
public ExportDatabaseFileTask() {
}
//delete if necessary
//private final ProgressDialog dialog = new ProgressDialog(null);
// can use UI thread here
protected void onPreExecute() {
// this.dialog.setMessage("Exporting database...");
// this.dialog.show();
}
// automatically done on worker thread (separate from UI thread)
protected Boolean doInBackground(final String... args) {
//original database file location
File dbFile = new File(Environment.getDataDirectory()
+ "/com.example.multapply/databases/MultapplyDatabase.db");
//the destination file location
File exportDir = new File(Environment.getExternalStorageDirectory(), "");
if (!exportDir.exists()) {
exportDir.mkdirs();
}
File file = new File(exportDir, dbFile.getName());
try {
file.createNewFile();
this.copyFile(dbFile, file);
return true;
} catch (IOException e) {
Log.e("mypck", e.getMessage(), e);
return false;
}
}
// can use UI thread here
protected void onPostExecute(final Boolean success) {
// if (this.dialog.isShowing()) {
// this.dialog.dismiss();
// }
// if (success) {
// Toast.makeText( null, "Export successful!", Toast.LENGTH_SHORT)
// .show();
// } else {
// Toast.makeText(null, "Export failed", Toast.LENGTH_SHORT).show();
// }
}
void copyFile(File src, File dst) throws IOException {
FileChannel inChannel = new FileInputStream(src).getChannel();
FileChannel outChannel = new FileOutputStream(dst).getChannel();
try {
inChannel.transferTo(0, inChannel.size(), outChannel);
} finally {
if (inChannel != null)
inChannel.close();
if (outChannel != null)
outChannel.close();
}
}
}
此類被實例化和調用的代碼:
/**
* CRUD Operations
* */
// Inserting Contacts
Log.d("Insert: ", "Inserting ..");
db.addScore(new Score(UserName.getUserName(), score, System.currentTimeMillis() ));
//attempting to export the file to the sd card
ExportDatabaseFileTask task = new ExportDatabaseFileTask();
task.execute();
注意:我之前遇到過與我在這里詢問的“Writing exception to parcel”相關的錯誤,但是現在我刪除了這個錯誤:
<uses-permission android:name="android.permission.INTERACT_ACROSS_USERS_FULL"/>
從清單中。
不要使用Environment.getDataDirectory()
來獲取您的數據位置。 使用您的Context
對象來獲取您的數據庫和/或數據位置。 請參閱方法getDir()
、 getDatabasePath()
和getExternalFilesDir()
。
如果將 DB 文件復制到 SD 卡,例如復制到:/sdcard/MyApp/Databases/MultapplyDatabase.db,則需要從該位置打開該文件。
File dbFile = new File("/sdcard/MyApp/Databases/MultapplyDatabase.db");
在您的清單應用程序中添加以下內容。
android:requestLegacyExternalStorage="true"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.