Android的错误:没有这样的文件或目录? [英] Android error: No such file or directory?
问题描述
我想通过我的Android应用程序,这是工作在模拟器上创建一个数据库。
I am trying to create a database through my android application, which is working on an emulator.
不过,正如我不能直接查看数据库我的三星设备上(它的根源并非是)我想数据库文件传输到SD卡。
However, as I cant view the database directly on my Samsung device (it is not rooted) I am trying to transfer the database file to the SD card.
该文件没有成功转移到SD卡,当我尝试在Eclipse中打开它,它不存在。
The file is not successfully transferring to the SD card, when I try and open it in eclipse it is not there.
我得到在logcat中下面的错误:
I am getting the following error in 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
相关code:
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();
}
}
}
code,其中这个类实例化,并呼吁:
Code where this class is instantiated and called:
/**
* 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();
请注意:我是previously获得比与写入例外包裹,我问了一下<一个错误href=\"http://stackoverflow.com/questions/24744877/android-writing-exception-to-parcel?noredirect=1#comment38391700_24744877\">here,但这个错误现在已经走了,我删除:
Note: I was previously getting than error relating to "Writing exception to parcel" that I asked about here, but this error has gone now that I removed:
<uses-permission android:name="android.permission.INTERACT_ACROSS_USERS_FULL"/>
从清单。
推荐答案
不要使用 Environment.getDataDirectory()
来获取你的数据的位置。用你的上下文
对象,以获取你的数据库和/或数据的位置(S)。请参阅方法 GETDIR()
, getDatabasePath()
和 getExternalFilesDir()
。
Don't use Environment.getDataDirectory()
to get your data location. Use your Context
object to get your database and/or data location(s). See the methods getDir()
, getDatabasePath()
, and getExternalFilesDir()
.
这篇关于Android的错误:没有这样的文件或目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!