Thursday, January 12, 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.setTransactionSuccessful();
}
finally{
    db.endTransaction();
}
db.close();

Reference: http://stackoverflow.com/questions/3501516/android-sqlite-database-slow-insertion

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 exportPATH=${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

Monday, January 9, 2012

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";
}

// Others
/*
PDF("PDF", new byte[][] { { 0x25, 0x50, 0x44, 0x46 } }),
JPG("JPG", new byte[][] { { (byte) 0xff, (byte) 0xd8, (byte) 0xff, (byte) 0xe0 } }),
RAR("RAR", new byte[][] { { 0x52, 0x61, 0x72, 0x21 } }),
GIF("GIF", new byte[][] { { 0x47, 0x49, 0x46, 0x38 } }),
PNG("PNG", new byte[][] { { (byte) 0x89, 0x50, 0x4e, 0x47 } }),
ZIP("ZIP", new byte[][] { { 0x50, 0x4b } }),
TIFF("TIFF", new byte[][] { { 0x49, 0x49 }, { 0x4D, 0x4D } }),
BMP("BMP", new byte[][] { { 0x42, 0x4d } });
*/

[ROM] Samsung S7 Stock Firmware BRI (Taiwan台灣)

Latest ROM: G930FXXU1DQEU Download: https://kfhost.net/tpl/firmwares.php?record=B7E6DE774E3811E7963AFA163EE8F90B Reference: http://...