使用Base64和Google Protobuf进行Android OutOfMemoryError

时间:2015-11-16 07:19:42

标签: android base64 out-of-memory bytearray protocol-buffers

我使用Google protobuf将数据存储到SQLite中,并使用SyncAdapter app定期向服务器发送数据。

我正在使用Base64将字节数组转换为String。

/**
     * @param context
     * @param payLoad
     */
    public static void saveData(final Context context, byte[] payLoad) {
        try {
            synchronized (LOCK) {
                final String deviceEncodedString = Base64.encodeToString(payLoad, Base64.DEFAULT);
                final ContentValues deviceContentValues = new ContentValues();
                deviceContentValues.put(DatabaseHelper.KEY_DATA, deviceEncodedString);
                context.getContentResolver().insert(CONTENT_URI, deviceContentValues);
            }
        } catch (Exception e) {
            Logger.e(TAG, e.getMessage(), e);
        }
    }

在将其发送到服务器时,我正在对其进行解码,当时我遇到了错误。

byte[] payload = null;
try {
     payload = Base64.decode(journalBase64.getJournal(), Base64.DEFAULT);
} catch (InvalidProtocolBufferException e) {
     Logger.e(TAG, e.getMessage(), e);
} catch (Exception e) {
     Logger.e(TAG, e.getMessage(), e);
}

错误日志: -

Fatal Exception: java.lang.OutOfMemoryError: Failed to allocate a 144 byte allocation with 8388608 free bytes and 316MB until OOM; failed due to fragmentation (required continguous free 16384 bytes for a new buffer where largest contiguous free 12288 bytes)
       at android.util.Base64.decode(Base64.java:171)
       at android.util.Base64.decode(Base64.java:136)
       at android.util.Base64.decode(Base64.java:118)
       at co.pointwise.android.sdk.sync.SynchAdapter.onPerformSync(SynchAdapter.java:80)
       at android.content.AbstractThreadedSyncAdapter$SyncThread.run(AbstractThreadedSyncAdapter.java:259)

0 个答案:

没有答案