Skip to content

Instantly share code, notes, and snippets.

@brandhill
brandhill / gist:6587d02bd88b95b44668
Created February 3, 2015 09:08
Android 的 Bitmap 轉成 Base64 並存入 SQLite 資料庫的方法
//Android的Bitmap轉成Base64並存入資料庫的方法
// 先把 bitmpa 轉成 byte
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream );
byte bytes[] = stream.toByteArray();
// Android 2.2以上才有內建Base64,其他要自已找Libary或是用Blob存入SQLite
String base64 = Base64.encodeToString(bytes, Base64.DEFAULT); // 把byte變成base64
// 再來是轉回來, 把Base64變回bytes
@brandhill
brandhill / ScrimUtil.java
Created November 26, 2014 10:37
Better gradient scrims
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Shader;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.PaintDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RectShape;
import android.util.FloatMath;
import android.view.Gravity;
@brandhill
brandhill / gist:05a0177ddcee3fd44500
Last active January 30, 2023 09:48
Android Read Assets file as string
private String loadAssetTextAsString(Context context, String name) {
BufferedReader in = null;
try {
StringBuilder buf = new StringBuilder();
InputStream is = context.getAssets().open(name);
in = new BufferedReader(new InputStreamReader(is));
String str;
boolean isFirst = true;
while ( (str = in.readLine()) != null ) {
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.*;
public class JsonHelper {
public static Object toJSON(Object object) throws JSONException {
if (object instanceof Map) {
JSONObject json = new JSONObject();
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;
public class DynamicImageView extends ImageView {
public DynamicImageView(final Context context, final AttributeSet attrs) {
super(context, attrs);
}
package com.feizan.android.snowball.ui.imageview;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.RelativeLayout;
/**
/**
* Copyright 2013 Bo Wang
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
/**
* Copyright 2013 Bo Wang
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@brandhill
brandhill / gist:3e59f8bdca5050e6ec5c
Created August 18, 2014 06:01
Merging two arrayLists into a new arrayList, with no duplicates (Java)
// 方法 1
ArrayList al = new ArrayList();
// add elements to al, including duplicates
HashSet hs = new HashSet();
hs.addAll(al);
al.clear();
al.addAll(hs);
@brandhill
brandhill / gist:666f8d64371ade54ddd8
Last active August 29, 2015 14:05
android - avoid clipboard manager crash (only) on android 2.x
//Clipboard API has changed on level 11 of Android SDK. Here is some code to handle both versions from arinkverma.
// get text
@SuppressWarnings("deprecation")
public void putText(String text){
int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES. HONEYCOMB) {
android.text.ClipboardManager clipboard = (android.text.ClipboardManager) activity.getSystemService(Context.CLIPBOARD_SERVICE);
clipboard.setText(text);