我一直在研究以下代码,该代码在 android 平板电脑上打开存储的图像,将其解码为位图,然后将其转换为 base64 字符串,以便可以将其存储在 SQLite DB 中。
据我所知,该事务泄露了多达 2MB 的数据,多次调用此函数会占用越来越多的未正确垃圾收集的内存。
填充 origin_photo 后,它大约占用 49kb,一旦转换为 base64,它占用大约 65kb。
photo.insert 函数是 base64 的简单 SQLite 插入以及一些小信息。我不认为这是问题的一部分。
此功能完成后,我还在 log cat 中收到一条消息“跳过 57 帧!应用程序可能在其主线程上做了太多工作”(这没有任何断点会减慢代码速度)
我可能弄错了,它是靠近此代码运行的不同部分导致泄漏,但这似乎是最有可能的候选者。任何帮助都非常感谢。
更新: base64.encodeBytes 函数取自
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; }
}