使用SQLite而不是.txt文件会导致OutOfMemory错误

时间:2013-03-28 10:04:02

标签: android database sqlite memory-management textwatcher

在我的应用程序中,我使用.txt文件来保存地理位置和字符串,因此我可以删除一行,而不是从.txt中删除一堆换行符。所以我把它放在测试中只是为了保存数据,保存得很好,但是当我加载数据时,我得到一个内存不足的位图,它是96x96解码文件,这对我来说没有意义。

这是logcat:

03-28 09:48:30.059: E/dalvikvm-heap(4607): Out of memory on a 46096-byte  allocation.
03-28 09:48:30.059: I/dalvikvm(4607): "main" prio=5 tid=1 RUNNABLE
03-28 09:48:30.059: I/dalvikvm(4607):   | group="main" sCount=0 dsCount=0   obj=0x40a719a0 self=0x2a00bba8
03-28 09:48:30.059: I/dalvikvm(4607):   | sysTid=4607 nice=0 sched=0/0 cgrp=apps    handle=1073849308
03-28 09:48:30.070: I/dalvikvm(4607):   | state=R schedstat=( 9701142608 17839341793 3050 ) utm=828 stm=142 core=0
03-28 09:48:30.070: I/dalvikvm(4607):   at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
03-28 09:48:30.070: I/dalvikvm(4607):   at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:529)
03-28 09:48:30.070: I/dalvikvm(4607):   at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:302)
03-28 09:48:30.070: I/dalvikvm(4607):   at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:328)
03-28 09:48:30.070: I/dalvikvm(4607):   at com.apps.gonefishing.Information.onCreate(Information.java:80)
03-28 09:48:30.070: I/dalvikvm(4607):   at android.app.Activity.performCreate(Activity.java:5104)
03-28 09:48:30.070: I/dalvikvm(4607):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-28 09:48:30.070: I/dalvikvm(4607):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-28 09:48:30.070: I/dalvikvm(4607):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-28 09:48:30.081: I/dalvikvm(4607):   at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-28 09:48:30.081: I/dalvikvm(4607):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-28 09:48:30.090: I/dalvikvm(4607):   at android.os.Handler.dispatchMessage(Handler.java:99)
03-28 09:48:30.090: I/dalvikvm(4607):   at android.os.Looper.loop(Looper.java:137)
03-28 09:48:30.090: I/dalvikvm(4607):   at android.app.ActivityThread.main(ActivityThread.java:5039)
03-28 09:48:30.090: I/dalvikvm(4607):   at java.lang.reflect.Method.invokeNative(Native Method)
03-28 09:48:30.090: I/dalvikvm(4607):   at   java.lang.reflect.Method.invoke(Method.java:511)
03-28 09:48:30.090: I/dalvikvm(4607):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-28 09:48:30.090: I/dalvikvm(4607):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-28 09:48:30.090: I/dalvikvm(4607):   at dalvik.system.NativeStart.main(Native Method)
03-28 09:48:30.110: D/skia(4607): --- decoder->decode returned false
03-28 09:48:30.110: D/AndroidRuntime(4607): Shutting down VM
03-28 09:48:30.110: W/dalvikvm(4607): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
03-28 09:48:30.140: E/AndroidRuntime(4607): FATAL EXCEPTION: main
03-28 09:48:30.140: E/AndroidRuntime(4607): java.lang.OutOfMemoryError
03-28 09:48:30.140: E/AndroidRuntime(4607):     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
03-28 09:48:30.140: E/AndroidRuntime(4607):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:529)
03-28 09:48:30.140: E/AndroidRuntime(4607):     at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:302)
03-28 09:48:30.140: E/AndroidRuntime(4607):     at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:328)
03-28 09:48:30.140: E/AndroidRuntime(4607):     at com.apps.gonefishing.Information.onCreate(Information.java:80)
03-28 09:48:30.140: E/AndroidRuntime(4607):     at android.app.Activity.performCreate(Activity.java:5104)
03-28 09:48:30.140: E/AndroidRuntime(4607):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-28 09:48:30.140: E/AndroidRuntime(4607):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-28 09:48:30.140: E/AndroidRuntime(4607):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-28 09:48:30.140: E/AndroidRuntime(4607):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-28 09:48:30.140: E/AndroidRuntime(4607):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-28 09:48:30.140: E/AndroidRuntime(4607):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-28 09:48:30.140: E/AndroidRuntime(4607):     at android.os.Looper.loop(Looper.java:137)
03-28 09:48:30.140: E/AndroidRuntime(4607):     at android.app.ActivityThread.main(ActivityThread.java:5039)
03-28 09:48:30.140: E/AndroidRuntime(4607):     at java.lang.reflect.Method.invokeNative(Native Method)
03-28 09:48:30.140: E/AndroidRuntime(4607):     at java.lang.reflect.Method.invoke(Method.java:511)
03-28 09:48:30.140: E/AndroidRuntime(4607):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-28 09:48:30.140: E/AndroidRuntime(4607):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-28 09:48:30.140: E/AndroidRuntime(4607):     at dalvik.system.NativeStart.main(Native Method)

我注意到的一件事是,每次堆消息出现时,只有5%到6%可用,所以我启动模拟器擦除数据,这是一回事。这是一个api 17模拟器,当应用程序与文本文件一起运行时,它运行得很好。

接下来我要尝试的是;即使我打开数据库阅读我正在使用:

db = dbHelper.getWritableDatabase();

所以我要把它改成getReadableDatabase

这不是一个很大的改变,而是在这个错误之前的活动中,它完成了一切。在TextWatcher的画布上,有两个位图来回发送。我认为它只需要内存2位图320X480与.txt文件运行良好。

非常感谢任何建议,谢谢你的时间。

 Cursor cursor;
        db.read();
        cursor = db.getAllEntries();
        boolean go = cursor.moveToFirst();
        while(cursor.isAfterLast() != true){
            Drawable imagebig = null;
            Bitmap photobig = null;
            int lat = cursor.getInt(MyDBAdapter.LATITUDE_COLUMN);
            int lng = cursor.getInt(MyDBAdapter.LONGITUDE_COLUMN);
            GeoPoint recall = new GeoPoint(lat,lng);
            File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Gone");
            file = new File(file.getPath(),"Thumbs");
            String string = cursor.getString(MyDBAdapter.FILE_COLUMN);
            File file1 = new File(file.getPath() + File.separator + "thumb" + string);

            Uri uri = Uri.fromFile(file1);
            if(file1.exists()){
             //photobig = BitmapFactory.decodeFile(file1.getAbsolutePath());
             uri = Uri.fromFile(file1);

             try {
                photobig = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


        }
        db.close();
        cursor.close();

2 个答案:

答案 0 :(得分:3)

你有一个无限的while循环。游标需要先进。

添加cursor.moveToNext();到你的while循环。

答案 1 :(得分:0)

并改变这个

while(cursor.isAfterLast() != true){

表示

while(!cursor.isAfterLast()){
伤害了。 *不要采取不好的方式:)