Posts

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...

Convert DateTime SQLite to GMT DateTime SQLite

static public String convertDateTimeToGMT(String in) { String out = in; SimpleDateFormat sdf = new SimpleDateFormat(Utility.getDBDateFormat()); Date iDate = null; try { iDate = sdf.parse(in); } catch (ParseException e) { Log.e(TAG, e.getMessage()); return in; } long newd = iDate.getTime() - sdf.getTimeZone().getRawOffset(); SimpleDateFormat osdf = new SimpleDateFormat(Utility.getDBDateFormat()); out = osdf.format(new Date(newd)); return out; }

Create JAR suitable for use with Dalvikvm

1. Create Helloworld.java: package org.apache; public class HelloWorld { public static void main(String[] args) { } } 2. Compile java: javac -d . -g Helloworld.java 3. Package Java Into a temporary jar jar -cvf Temp.jar * 4. Use DX to generate classes.dex from Temp.jar dx --dex --output=classes.dex Temp.jar 5. Use aapt to create new Jar (CmdLine.jar) suitable for use with Dalvikvm aapt add CmdLine.jar classes.dex 6. Push Jar to folder: adb shell     mount -o remount /system     exit adb push CmdLine.jar /system/framework/ 7. Restart: adb reboot

Create your own shared-library

Below is the step of creating own shared-library: Create JAVA file, ex: c:\com.myactivity.java Compress to JAR, with command: jar cf c:\com.myactivity.jar c:\com.myactivity.java JAR is our library file. Create XML file (permission file), ex: c:\com.myactivity.xml, as below: <permissions> <library name="com.myactivity" file="/system/framework/com.myactivity.jar" /> </permissions>  Root the OS image: adb root (We assume that the image can be rooted.) Mount /system, commands are as belows: adb shell mount –o remount /system exit  Push JAR and XML file to OS image: adb push c:\com.myactivity.jar /system/framework/ adb push c:\com.myactivity.xml /system/etc/permissions/  Reboot device. After rebooted, the shared-library can be used correctly. Try using to test the library. <uses-library android:name="com.myactivity" android:required="true" /> 

Shared Folder settings between Windows7 and uBuntu guest OS in VMWare

PS: Please make sure that your VMTools is installed correctly on the uBuntu Guest. http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1022525 Virtual Machine Settings Folder sharing = Always Enabled. Make sure you have at least one Folder shared between the host and guest. For example: my-D-drive On the uBuntu Guest check /mnt/hgfs that you can access your shared folder  If you cannot see the folder, then skip it. Next step will create the folder automatically. update your fstab using the details below: Open terminal and type: gksu gedit /etc/fstab Add below to the end of the file: .host:/{shared-folder} /{path-to-mount-on} vmhgfs defaults,ttl=5,uid=1000,gid=1000 0 0 Change {shared-folder} to the shared folder of Virtual Machine Settings (my-D-drive). Change {path-to-mount-on} to the folder inside uBuntu (/mnt/hgfs/my-D-drive). Restart your vm. You may need to restart few times or get error messa...