1

我一直在研究以下代码,该代码在 android 平板电脑上打开存储的图像,将其解码为位图,然后将其转换为 base64 字符串,以便可以将其存储在 SQLite DB 中。

据我所知,该事务泄露了多达 2MB 的数据,多次调用此函数会占用越来越多的未正确垃圾收集的内存。

填充 origin_photo 后,它大约占用 49kb,一旦转换为 base64,它占用大约 65kb。

photo.insert 函数是 base64 的简单 SQLite 插入以及一些小信息。我不认为这是问题的一部分。

此功能完成后,我还在 log cat 中收到一条消息“跳过 57 帧!应用程序可能在其主线程上做了太多工作”(这没有任何断点会减慢代码速度)

我可能弄错了,它是靠近此代码运行的不同部分导致泄漏,但这似乎是最有可能的候选者。任何帮助都非常感谢。

更新: base64.encodeBytes 函数取自

http://iharder.net/base64

public void savePhoto() 
{
    try
    {
        String[] projection = { MediaStore.Images.Media.DATA };
        //Get last captured image in db
        String capturedImageFilePath = null;
        try
        {
            //NOTE: The warning on activity.managedQuert states you must NOT close the cursor it creates yourself 
            Cursor cursor = activity.managedQuery(mCapturedImageURI, projection, null, null, null); 
            int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
            cursor.moveToFirst(); 
            //Get file path from last stored photo
            capturedImageFilePath = new String(cursor.getString(column_index_data));
            //cursor.close();
        }
        catch(IllegalArgumentException e)
        {
            ErrorCodes.CreateError(ErrorCodes.DCDF_SAVE_PHOTO_GET_IMAGE_FILE_PATH, this.getActivity());
        }
        catch(NullPointerException e)
        {
            ErrorCodes.CreateError(ErrorCodes.DCDF_SAVE_PHOTO_GET_IMAGE_FILE_PATH, this.getActivity());
        }
        catch(Exception e) 
        {
            ErrorCodes.CreateError(ErrorCodes.DCDF_SAVE_PHOTO_GET_IMAGE_FILE_PATH, this.getActivity()); 
        }

        int orientation = -1;
        //Get Exif data from current image and store orientation
        //Note exif data will be stripped when this filepath is turned
        //into a bitmap
        try 
        {
            ExifInterface e = new ExifInterface(capturedImageFilePath);
            orientation = e.getAttributeInt("Orientation", -1);
        }
        catch (IOException ioe) 
        {
            ioe.printStackTrace();
            ErrorCodes.CreateError(ErrorCodes.DCDF_SAVE_PHOTO_EXIF_DATA_IO, this.getActivity());
        }
        catch(Exception e) { ErrorCodes.CreateError(ErrorCodes.DCDF_SAVE_PHOTO_EXIF_DATA_GENERAL, this.getActivity()); }

        //Decode current photo into a Bitmap
        Bitmap b = BitmapFactory.decodeFile(capturedImageFilePath);

        if(b == null)
        {
            Log.d("activitycrossellcalls", "error open " + capturedImageFilePath);
            ErrorCodes.CreateError(ErrorCodes.DCDF_SAVE_PHOTO_NULL_BITMAP, this.getActivity());
        }
        else 
        {
            int width = -1;
            int height = -1;

            width = b.getWidth();
            height = b.getHeight();
            Log.d("activitycrossellcalls", "w: "+width+", h:"+height);
            //Scale down if too big
            int max = (width > height)?width:height;
            float ratio = 1;
            if(max > MAX_IMAGE_SIZE)
                ratio = (float)max/(float)MAX_IMAGE_SIZE;
            width /= ratio;
            height /= ratio;

            b = Photos.getResizedBitmap(b, height, width);

            Log.i("activitycrossellcalls", "new w: " + width + ", h: " + height);

            // Encode
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            b.compress(Bitmap.CompressFormat.JPEG, 70, baos);
            b.recycle();


            byte[] origin_photo = null;

            origin_photo = baos.toByteArray();

            // Insert               
            Photo photo = null;
            try
            {
                photo = new Photo();
                photo.base64 = Base64.encodeBytes(origin_photo);
                photo.call = DailyCallsDetailsActivity.call.id;
                photo.tag_id = TaggingActivity.currentTag.id;
                photo.orientation = orientation;
            }
            catch(Exception e) { ErrorCodes.CreateError(ErrorCodes.DCDF_SAVE_PHOTO_INIT, this.getActivity()); }

            photo.insert();
        }
    }
    catch(Exception e) { ErrorCodes.CreateError(ErrorCodes.DCDF_SAVE_PHOTO, this.getActivity()); }
}        

.

public static Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) 
{
    try
    {
        int width = bm.getWidth();
        int height = bm.getHeight();
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        // CREATE A MATRIX FOR THE MANIPULATION
        Matrix matrix = new Matrix();
        // RESIZE THE BIT MAP
        matrix.postScale(scaleWidth, scaleHeight);
        // RECREATE THE NEW BITMAP
        Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
        bm.recycle();
        return resizedBitmap;
    }
    catch(Exception e) { ErrorCodes.ReportError(ErrorCodes.PHOTS_GET_RESIZED_BITMAP); return null; }
}
4

3 回答 3

3

我想我找到了

Bitmap b = BitmapFactory.decodeFile(capturedImageFilePath);
...
b = Photos.getResizedBitmap(b, height, width);

你没有回收原来的 b。这不应该是永久性泄漏,但肯定会导致 GC 崩溃。

您可能还需要考虑使用 LRUCache 自动循环进出位图。

于 2013-07-03T11:21:42.037 回答
2

您解码可能很大的图像。在 savePhoto() 中这一行之后检查 Bitmap b 的大小:

    Bitmap b = BitmapFactory.decodeFile(capturedImageFilePath);

之后你缩小它,所以当你保存到数据库时它会显得更小。我的猜测是您的源文件很大,这导致内存比您预期的要多。

此外,对于来自 logcat 的“跳过帧”消息,通常会在您在 UI 线程上执行长时间运行的操作时发生。确保您的方法正在从 UI 线程中调用,可能在AsyncTask.

于 2013-07-03T11:06:46.970 回答
0

您没有关闭光标。可能不是唯一的问题,但它是其中之一。

于 2013-07-03T11:01:08.583 回答