Monday, September 16, 2013

Write and Read (Serialize) Object to memory

If you want to save object like HashMap, Array and so on to application memory,
use code below. Please note, all objects fields should be Serializable.

Types like String, int, HashMap... are already serializable, but if you create a class of your own, you should verify that that it implements Serializable, and so all of his content recursively.

Write to file:
    /**
     * Writes Serializable object into a file
     */
    public static void writeObjectToFile(Context context, Object object, String filename)
    {
        ObjectOutputStream objectOut = null;
        try
        {
            FileOutputStream fileOut = context.openFileOutput(filename,Activity.MODE_PRIVATE);
            objectOut = new ObjectOutputStream(fileOut);
            objectOut.writeObject(object);
            fileOut.getFD().sync();

        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (objectOut != null)
            {
                try
                {
                    objectOut.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
    }
 
 
Read from file:
    /**
     * Reads a Serializable object from a file
     */
    public static Object readObjectFromFile(Context context, String filename)
    {
        ObjectInputStream objectIn = null;
        Object object = null;
        try
        {
            FileInputStream fileIn = context.getApplicationContext().openFileInput(filename);
            objectIn = new ObjectInputStream(fileIn);
            object = objectIn.readObject();

        }
        catch (FileNotFoundException e)
        {
            // Do nothing
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (objectIn != null)
            {
                try
                {
                    objectIn.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }

        return object;
    }

Monday, September 9, 2013

How to use Google Maps V2 - Keystore orientation



1. Locate your keystore file:

    For debugging you should use the debug keystore file located in:
    C:\Users\USER_NAME_HERE\.android\debug.keystore

    For release version you should use the generated keystore
    (the same one that you use when you'll sign your applicatoin for distrebution)

2. Execute command (cmd) and extract SHA1 string:

    For debug:
keytool -v -list -alias androiddebugkey -keystore 
YOUR_DEBUG_KEYSTORE_FILE_PATH_HERE -storepass android 
-keypass android

    For release:
keytool -v -list -alias YOUR_ALIAS_HERE -keystore 
YOUR_KEYSTORE_FILE_PATH_HERE -storepass YOUR_GENERAL_PASSWORD_HERE 
-keypass YOUR_ALIAS_PASSWORD_HERE

3. Open https://code.google.com/apis/console/

4. Go to services.

5. Enable google maps V2.

6. Go to API access menu item.

7. Press on "Create new Android key".

8. Paste your sha1 code with the application package:
(e.g. 40:DC:19:FF:11:33:B9:61:64:54:79:7A:84:5C:C8:F8:AF:CC:FC:1C;
com.company.appname)

9. Copy your API key from the Simple API Access    
    (should look like : AIzzSyACccKZeD-4lmBrtZI0JSrxBOAMnK1oEIk).

10. Paste your API key in your AndroidManifest.xml file:
<meta-data android:name="com.google.android.maps.v2.API_KEY"
 android:value="AIzzSyACccKZeD-4lmBrtZI0JSrxBOAMnK1oEIk"/>