Sqlite数据库大小与本地db大小比较

时间:2014-05-18 06:16:08

标签: android sqlite

我有SDcard数据库文件和app sqlite数据库如何比较大小?当我在下面运行此代码时,总是崩溃..

   private ContextWrapper context;


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    File extStore = Environment.getExternalStorageDirectory();
    File myFile = new File(extStore.getAbsolutePath() + externalPath
            + FileName);

    File f = context.getDatabasePath(DB_PATH);
    long dbSize = f.length();

    if (dbSize <= myFile.length()) {

            //code here


    } else {



    }

感谢您的帮助...

 05-18 02:33:05.410: E/AndroidRuntime(1085): FATAL EXCEPTION: main
 05-18 02:33:05.410: E/AndroidRuntime(1085): Process:    in.wptrafficanalyzer.searchdialogdemo, PID: 1085
 05-18 02:33:05.410: E/AndroidRuntime(1085): java.lang.RuntimeException: Unable to start  activity  ComponentInfo{in.wptrafficanalyzer.searchdialogdemo/in.wptrafficanalyzer.searchdialogdemo.Lite Activity}: java.lang.NullPointerException
 05-18 02:33:05.410: E/AndroidRuntime(1085):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
 05-18 02:33:05.410: E/AndroidRuntime(1085):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
 05-18 02:33:05.410: E/AndroidRuntime(1085): Caused by: java.lang.NullPointerException
 05-18 02:33:05.410: E/AndroidRuntime(1085):    at in.wptrafficanalyzer.searchdialogdemo.LiteActivity.onCreate(LiteActivity.java:65)
 05-18 02:33:05.410: E/AndroidRuntime(1085):    at android.app.Activity.performCreate(Activity.java:5231)
 05-18 02:33:05.410: E/AndroidRuntime(1085):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
 05-18 02:33:05.410: E/AndroidRuntime(1085):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
 05-18 02:33:05.410: E/AndroidRuntime(1085):    ... 11 more

1 个答案:

答案 0 :(得分:0)

您的context为空,因为您尚未初始化它。

由于您正在运行某个活动的方法,因此将Context存储为成员变量毫无意义。请改用this。取代

context.getDatabasePath(DB_PATH);

只是

getDatabasePath(DB_PATH);