Code example for ByteArrayOutputStream

Methods: closetoByteArrayflush

            try { 
                // Try go guesstimate how much space the icon will take when 
                // serialized to avoid unnecessary allocations/copies during 
                // the write. 
                int size = bitmap.getWidth() * bitmap.getHeight() * 4;
                ByteArrayOutputStream out = new ByteArrayOutputStream(size);
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
                ContentValues values = new ContentValues();
                values.put(PhotoDatabaseHelper.FIELD_APPWIDGET_ID, appWidgetId);
                values.put(PhotoDatabaseHelper.FIELD_PHOTO_BLOB, out.toByteArray());
                SQLiteDatabase db = getWritableDatabase();
                db.insertOrThrow(PhotoDatabaseHelper.TABLE_PHOTOS, null, values);
                success = true;
            } catch (SQLiteException e) {
                Log.e(TAG, "Could not open database", e);
Experience pair programming with AI  Get Codota for Java