Posts

Showing posts from January, 2012

Speed up SQLite insert

Using android.database.DatabaseUtils.InsertHelper class. Sample code: SQLiteDatabse db = getWriteableDatabase(); //use the db you would normally use for db.insert, and the "table_name" //is the same one you would use in db.insert() InsertHelper iHelp = new InsertHelper(db, "table_name"); //Get the indices you need to bind data to //Similar to Cursor.getColumnIndex("col_name"); int first_index = iHelp.getColumnIndex("first"); int last_index = iHelp.getColumnIndex("last"); try{ db.beginTransaction(); for(int i=0 ; i<num_things ; ++i) { //need to tell the helper you are inserting (rather than replacing) iHelp.prepareForInsert(); //do the equivalent of ContentValues.put("field","value") here iHelp.bind(first_index, thing_1); iHelp.bind(last_index, thing_2); //the db.insert() equilvalent iHelp.execute(); } db.setTransactionSuccessfu...

Set up ADB environment in Mac OSX

Open Terminal Type cd ~ this will take you to your home directory Type touch . profile this will create a hidden file named profile Type open - e . profile this will open the file you just created in TextEdit In the file, type export PATH = $ { PATH }:/ pathToTheAndroidSdkFolder / android - sdk - mac_86 / platform - tools Save file, close TextEdit, Quit Terminal, and Relaunch Terminal NOTE: By creating an environment variable you won't need to cd to the Android/tools folder every time you want to run ADB Reference:  http://esausilva.com/2010/10/02/how-to-set-up-adb-android-debug-bridge-in-mac-osx/

Get mimetype from header file

// return: jpeg, png, bmp, unknown public static String getMimeTypeFileByHeader(String realPath) { File f = new File(realPath); try { InputStream in = new FileInputStream(f); byte[] HEADERJPEG = new byte[] {(byte) 0xFF, (byte) 0xD8, (byte) 0xFF}; byte[] HEADERBMP = new byte[] { 0x42, 0x4d }; byte[] HEADERPNG = new byte[] { (byte) 0x89, 0x50, 0x4e }; byte[] header = new byte[3]; int h = in.read(header); if (h==3 && Arrays.equals(header, HEADERJPEG)) { return "jpeg"; } if (h==3 && Arrays.equals(header, HEADERPNG)) { return "png"; } byte[] header2 = new byte[2]; for (int i=0; i<2; i++) header2[i] = header[i]; if (Arrays.equals(header2, HEADERBMP)) { return "bmp"; } } catch (Throwable e) {} return "unknown...