我正在开发一个Android应用程序,我必须从服务器下载sqlite数据库文件,并且必须从中获取信息。对于服务器,目前我正在使用我的Dropbox帐户,我正在执行下载和打开任务以在类AsyncTask中打开数据库文件。在doinbackground中,我正在下载数据库文件,在postExecute中,我打开下载的数据库。但是当我执行应用程序时,它成功下载了数据库文件,并在调用postExecute方法时显示错误。即当我尝试打开数据库文件时。
这是我的MainActivity。
package com.example.avinash.sqlite_re2;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.Button;
public class MainActivity extends Activity {
private SQLiteDatabase db;
private Cursor c;
private static String file_url = "https://www.dropbox.com/s/k0de92qc8hx2v6t/password.db?dl=0";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DownloadFileFromUrl download = new DownloadFileFromUrl(MainActivity.this);
download.execute(file_url);
}
}
这是我的AsyncTask类downloadFileFromUrl
package com.example.avinash.sqlite_re2;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;
/**
* Created by Avinash on 21-04-2016.
*/
public class DownloadFileFromUrl extends AsyncTask <String, String, String> {
private SQLiteDatabase db;
private Cursor c;
Context context;
DownloadFileFromUrl(Context context)
{
this.context= context;
}
File ParentDirectory = new File("data/data/com.example.avinash.sqlite_re2" + "/files/");
File outputFile = new File(ParentDirectory, "data.db");
@Override
protected void onPreExecute() {
super.onPreExecute();
if(!ParentDirectory.exists()) {
ParentDirectory.mkdir();
Toast.makeText(context, "Creating folder", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(context, "folder exist", Toast.LENGTH_SHORT).show();
}
}
@Override
protected String doInBackground(String... f_url) {
int count;
try {
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
InputStream input = new BufferedInputStream(url.openStream(), 8192);
OutputStream output = new FileOutputStream(outputFile);
byte data[] = new byte[1024];
while ((count = input.read(data)) != -1) {
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(String file_url) {
String path = "data/data/com.example.avinash.sqlite_re2" + "/files/";
String name = "data.db";
Toast.makeText(context , " is there ", Toast.LENGTH_SHORT).show();
db = SQLiteDatabase.openDatabase(path + name, null, 0);
}
这是我的清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.avinash.sqlite_re2">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
</manifest>
}
我已经处理好这些事情: - 1.互联网许可 2.读/写权限 3.我的数据库中的主键名为_id。 4.我在我的数据库中创建了表android_metadata。 我收到以下错误
04-26 23:30:26.999 31015-31015/? D/dalvikvm: Late-enabling CheckJNI
04-26 23:30:26.999 31015-31015/? D/dalvikvm: Try to disable coredump for pid 31015
04-26 23:30:26.999 31015-31015/? D/dalvikvm: Process 31015 nice name: com.example.avinash.sqlite_re2
04-26 23:30:26.999 31015-31015/? D/dalvikvm: Extra Options: not specified
04-26 23:30:27.229 31015-31015/com.example.avinash.sqlite_re2 E/IMGSRV: :0: PVRDRMOpen: TP3, ret = 46
04-26 23:30:27.239 31015-31015/com.example.avinash.sqlite_re2 E/IMGSRV: :0: PVRDRMOpen: TP3, ret = 50
04-26 23:30:27.239 31015-31015/com.example.avinash.sqlite_re2 E/IMGSRV: :0: PVRDRMOpen: TP3, ret = 51
04-26 23:30:27.249 31015-31015/com.example.avinash.sqlite_re2 E/IMGSRV: :0: PVRDRMOpen: TP3, ret = 51
04-26 23:30:27.249 31015-31015/com.example.avinash.sqlite_re2 E/IMGSRV: :0: PVRDRMOpen: TP3, ret = 51
04-26 23:30:27.259 31015-31015/com.example.avinash.sqlite_re2 E/IMGSRV: :0: PVRDRMOpen: TP3, ret = 53
04-26 23:30:27.289 31015-31015/com.example.avinash.sqlite_re2 D/OpenGLRenderer: Enabling debug mode 0
04-26 23:30:29.569 31015-31023/com.example.avinash.sqlite_re2 I/dalvikvm: Total arena pages for JIT: 11
04-26 23:30:29.569 31015-31023/com.example.avinash.sqlite_re2 I/dalvikvm: Total arena pages for JIT: 12
04-26 23:30:29.589 31015-31023/com.example.avinash.sqlite_re2 I/dalvikvm: Total arena pages for JIT: 13
04-26 23:30:31.499 31015-31015/com.example.avinash.sqlite_re2 E/SQLiteLog: (26) file is encrypted or is not a database
04-26 23:30:31.499 31015-31015/com.example.avinash.sqlite_re2 E/DefaultDatabaseErrorHandler: Corruption reported by sqlite on database: data/data/com.example.avinash.sqlite_re2/files/data.db
04-26 23:30:31.499 31015-31015/com.example.avinash.sqlite_re2 E/DefaultDatabaseErrorHandler: deleting the database file: data/data/com.example.avinash.sqlite_re2/files/data.db
04-26 23:30:31.499 31015-31015/com.example.avinash.sqlite_re2 E/SQLiteLog: (14) cannot open file at line 30175 of [000197cc4e]
04-26 23:30:31.499 31015-31015/com.example.avinash.sqlite_re2 E/SQLiteLog: (14) os_unix.c:30175: (2) open(//data/data/com.example.avinash.sqlite_re2/files/data.db) -
04-26 23:30:31.509 31015-31015/com.example.avinash.sqlite_re2 E/SQLiteDatabase: Failed to open database 'data/data/com.example.avinash.sqlite_re2/files/data.db'.
android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804)
at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:792)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)
at com.example.avinash.sqlite_re2.DownloadFileFromUrl.onPostExecute(DownloadFileFromUrl.java:88)
at com.example.avinash.sqlite_re2.DownloadFileFromUrl.onPostExecute(DownloadFileFromUrl.java:22)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:149)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
at dalvik.system.NativeStart.main(Native Method)
04-26 23:30:31.509 31015-31015/com.example.avinash.sqlite_re2 D/AndroidRuntime: Shutting down VM
04-26 23:30:31.509 31015-31015/com.example.avinash.sqlite_re2 W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x430f2140)
04-26 23:30:31.509 31015-31015/com.example.avinash.sqlite_re2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.avinash.sqlite_re2, PID: 31015
android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804)
at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:792)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)
at com.example.avinash.sqlite_re2.DownloadFileFromUrl.onPostExecute(DownloadFileFromUrl.java:88)
at com.example.avinash.sqlite_re2.DownloadFileFromUrl.onPostExecute(DownloadFileFromUrl.java:22)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:149)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
您拥有数据库的相对路径。此路径应以“/".
开头但不要硬编码这条路径;我会使用context.getFilesDir()。getAbsolutePath()代替。