How to change the Android emulator temporary directory

May 3rd, 2011 | Filed under Android

If you’d like the Android emulator to store its temporary files in a directory different from /tmp/android (maybe you’re sharing your computer with someone else that needs to run the emulator and you both want to run it at the same time), then there’s unfortunately no other way to do it but to change it directly in the emulator binary, since it’s hardcoded.

To do that, open up the binary in vim:

vim -b path-to-android-sdk/tools/emulator

Once the file is open, search for the “/tmp/android” string by typing the following and pressing ENTER:

/tmp\/android

Now press a, modify the path to whatever you like, as long as it contains the exact same amount of characters (make sure you don’t change anything else). After the path is changed, press Esc, then :wq, ENTER and start the emulator.

Applying page margins in EPUB with CSS

Apr 23rd, 2011 | Filed under EBooks, EPUB

Recently, pretty much all ebook readers adopted the @page CSS rule (including Adobe Digital Editions), making the proprietary Adobe Page Templates obsolete when trying to set margins.

Therefore, if you want to set a margin of 1.5em, all you need to do is use the following CSS rule:

@page {
    margin: 1.5em;
}

Calling C code from Java using JNA

Apr 22nd, 2011 | Filed under C, Java

I recently wrote an article about calling C code from Java using JNI. Another way to call C code from Java is to use Java Native Access, which makes things significantly easier.

First off, we’ll need to write the C library. Create a ctest.c file containing a simple helloFromC function:

/* ctest.c */
 
#include <stdio.h>
 
void helloFromC() {
    printf("Hello from C!\n");
}

Notice that this version is much simpler than the JNI version, and also not intrusive at all. Using JNA you can run code from any library without any of the additional boilerplate associated with JNI.

Now that we have the C source, let’s create a library:

gcc -o libctest.so -shared ctest.c

This is the standard way of creating a library, not specific to Java in any way. For details about creating the library on Mac or Windows, see the previously mentioned article.

Let’s now write the Java code in a HelloWorld.java file:

/* HelloWorld.java */
 
import com.sun.jna.Library;
import com.sun.jna.Native;
 
public class HelloWorld {
    public interface CTest extends Library {
        public void helloFromC();
    }
    static public void main(String argv[]) {
        CTest ctest = (CTest) Native.loadLibrary("ctest", CTest.class);
        ctest.helloFromC();
    }
}

After downloading the JNA package from the official Subversion repository an placing it in the same directory, we can now compile the Java application:

javac -classpath jna.jar HelloWorld.java

This should produce a HelloWorld.class file containing our compiled class. We can run it with:

java -classpath jna.jar:. HelloWorld

If everything works correctly, you should see:

Hello from C!

Importing photos from iPhone on Mac OS X

Apr 20th, 2011 | Filed under iPhone, Mac

To copy your photos from iPhone without synchronizing with iTunes:

  1. Open “Preview” (you can find it in the Applications folder)
  2. Choose File > Import from iPhone
  3. Choose the folder in the “Import To” dropdown
  4. Click “Import All” (or select the ones you wish to import and click “Import”)