我的程序应该如下工作:
1.Copy the new database in the program folder
2.Import the records from the old database to the new one.
但我出于某种原因得到了例外。为什么呢?
protected Object doInBackground(Object[] objects) {
String LOCAL_DATABASE_PATH = getApplicationInfo().dataDir + File.separator +
"databases" + File.separator;
File fileDir = new File(LOCAL_DATABASE_PATH);
if (!fileDir.exists())
fileDir.mkdirs();
File tempFile = new File(LOCAL_DATABASE_PATH + DATABASE_NAME);
try {
tempFile.createNewFile(); // here I catch exception
InputStream is = SplashActivity.this.getAssets().open(
DATABASE_NAME);
FileOutputStream os = new FileOutputStream(new File(
LOCAL_DATABASE_PATH, DATABASE_NAME));
int bufferLength = 0;
byte[] buffer = new byte[2048];
while ((bufferLength = is.read(buffer)) > 0) {
os.write(buffer, 0, bufferLength);
}
Preferences.getInstance(SplashActivity.this).
set(Preferences.IS_DATABASE_COPYING_ON_DEVICE, true);
is.close();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
我收到以下错误
java.io.IOException: open failed: EACCES (Permission denied)
08-28 09:32:27.977 32558-32558/com.DriverNotes.AndroidMobileClientTest D/libEGL﹕ loaded /system/lib/egl/libGLES_hawaii.so
08-28 09:32:27.977 32558-32587/com.DriverNotes.AndroidMobileClientTest W/System.err﹕ at java.io.File.createNewFile(File.java:948)
08-28 09:32:27.977 32558-32587/com.DriverNotes.AndroidMobileClientTest W/System.err﹕ at com.DriverNotes.AndroidMobileClientTest.SplashActivity$DataBaseLoadTask.doInBackground(SplashActivity.java:73)
08-28 09:32:27.977 32558-32587/com.DriverNotes.AndroidMobileClientTest W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:287)
08-28 09:32:27.977 32558-32587/com.DriverNotes.AndroidMobileClientTest W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:234)
08-28 09:32:27.977 32558-32587/com.DriverNotes.AndroidMobileClientTest W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
08-28 09:32:27.977 32558-32587/com.DriverNotes.AndroidMobileClientTest W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
08-28 09:32:27.977 32558-32587/com.DriverNotes.AndroidMobileClientTest W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
08-28 09:32:27.977 32558-32587/com.DriverNotes.AndroidMobileClientTest W/System.err﹕ at java.lang.Thread.run(Thread.java:856)
08-28 09:32:27.987 32558-32587/com.DriverNotes.AndroidMobileClientTest W/System.err﹕ Caused by: libcore.io.ErrnoException: open failed: EACCES (Permission denied)
08-28 09:32:27.987 32558-32587/com.DriverNotes.AndroidMobileClientTest W/System.err﹕ at libcore.io.Posix.open(Native Method)
08-28 09:32:27.987 32558-32587/com.DriverNotes.AndroidMobileClientTest W/System.err﹕ at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
08-28 09:32:27.987 32558-32587/com.DriverNotes.AndroidMobileClientTest W/System.err﹕ at java.io.File.createNewFile(File.java:941)
08-28 09:32:27.987 32558-32587/com.DriverNotes.AndroidMobileClientTest W/System.err﹕ ... 7 more
答案 0 :(得分:1)
使用您已创建的tempFile
创建fileDir
尝试时,请确保其存在。
File tempFile = new File(fileDir, DATABASE_NAME);
在创建新文件之前,您应该通过调用exits()
方法检查它是否已存在。然后你应该继续使用它而不是再打开它。或者至少在再次打开之前关闭它。
if(!tempFile.exists())
tempFile.createNewFile();
InputStream is = SplashActivity.this.getAssets().open(
DATABASE_NAME);
FileOutputStream os = new FileOutputStream(tempFile);
使用FileOutPutStream
创建tempFile
或关闭tempFile
以确保您的文件未被锁定。