0

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

这是日志:

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 个位图。我认为它只需要记忆 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();
4

2 回答 2

3

你有一个无限的while循环。光标需要前进。

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

于 2013-04-01T03:47:54.320 回答
0

并改变这个

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

为了那个原因

while(!cursor.isAfterLast()){

好痛。*不要以坏的方式对待它:)

于 2013-04-01T21:18:25.643 回答