Easy Image Chooser Library for Android

In almost all the Android apps that I have worked on, there has been a requirement for choosing an image or taking a snap and using the device’s camera.

Taking a snap, is rather straightforward to implement, but choosing an image from your gallery is sometimes a hard nut to crack. And to implement this correctly, you would always end up writing a lot of code. Assuming that you want to target all OSes, devices, folders etc.

For example, choosing a picture from the Camera folder of your phone is nice and easy. But, if you want to have the user chose an image from one of his picasa web albums, which he has synced on his phone, you will find a dead-end. Well, almost.

For myself, I must have at-least rewritten the same code, multiple number of times. Of course, the platform should help us in achieving this seemingly straightforward feature with as less code as possible. But, right now, it’s not possible.

So, I thought of creating a library which anyone could integrate within one’s own app, without really worrying about the nitty-gritties and the bugs, to implement or add this feature into his app. And there’s more. Most of the time, you would need a scaled down version of the chosen image to show a preview or use it in a listview. This library would give you 3 sizes, as of now.

  1. Original Size
  2. Thumbnail Size
  3. Thumbnail Smaller Size
The sizes are dynamically calculated, based on the original size of the image. It’s pretty rough here, but I am looking forward to improve that part.
This version of the library is pretty basic. But, I would be working on improving this library to add more functionality in the future.
You can find more information about this library here.
By using this library in the current state, you could handle all these cases/special cases with just a few lines of code. An example of this is shown below.
1. For choosing an image from gallery

imageChooserManager = new ImageChooserManager(this, ChooserType.REQUEST_PICK_PICTURE);

2. For capturing a picture using your camera

imageChooserManager = new ImageChooserManager(this, ChooserType.REQUEST_CAPTURE_PICTURE);

3. On Activity result, do this:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK &&
(requestCode == ChooserType.
requestCode == ChooserType.
imageChooserManager.submit(requestCode, data);

4. Implement the ImageChooserListener interface and override these methods:

public void onImageChosen(final ChosenImage image) {
runOnUiThread(new Runnable() {
public void run() {
if (image != null) {
// Use the image
// image.getFilePathOriginal();
// image.getFileThumbnail();
// image.getFileThumbnailSmall();
public void onError(final String reason) {
runOnUiThread(new Runnable() {
public void run() {
// Show error message

That’s all you need to code. And the library takes care of handling all kinds of images, and also generates 2 thumbnails which you could directly use. Let me know if you need to add any new features or if you find a bug. I will try to address those as soon as humanly possible. If you would like to contribute to this project, drop me a mail.

Streaming Radio Stations on Android

Streaming radio stations or audio files hosted on streaming servers on Android is pretty straight-forward. But then, Android has it’s limitations. It won’t stream just any file or radio station. In this post, I would not be specifying the formats or protocols that Android supports. Rather, this example is just a walk through of how the MediaPlayer class should be used to stream audio files/radio stations.

For an example here, I have used a SHOUTcast radio station. The URL for the source is:

The accompanying sample project contains a ProgressBar, two Buttons (for playing and stopping the MediaPlayer).

Before running the example, one should look into the documentation of the MediaPlayer class. A look at the state diagram would perhaps help you clear to understand how it actually works.

To initialize the MediaPlayer, you need a few lines of code. There you go:

MediaPlayer player = new MediaPlayer();


Now that the MediaPlayer object is initialized, you are ready to start streaming. Ok, not actually. You will need to issue the MediaPlayer’s prepare command. There are 2 variations of this.

  1. prepare(): This is a synchronous call, which is blocked until the MediaPlayer object gets into the prepared state. This is okay if you are trying to play local files that would take the MediaPlayer longer, else your main thread will be blocked.
  2. prepareAsync(): This is, as the name suggests, an asynchronous call. It returns immediately. But, that obvisouly, doesn’t mean that the MediaPlayer is prepared yet. You will still have to wait for it to get into the prepared state, but since this method will not block your main thread, you can use this method when you are trying to stream some content from somewhere else. You will get a callback, when the MediaPlayer is ready through onPrepared(MediaPlayer mp) method, and then, the playing can start.

So, for our example, the best choice would be:


You need to attach a listener to the MediaPlayer to receive the callback when it is prepared. This is the code for that.

player.setOnPreparedListener(new OnPreparedListener(){

            public void onPrepared(MediaPlayer mp) {


Once, it goes into the prepared state, you can now start playing. Simple???? Yes, of course. Just to wrap it up, to stop the MediaPlayer, you need to call the stop() method.

There are several other helper methods which lets you query the progress or status of the player. Jump to the docs page and you will find more information on them. You can checkout the source code here.

Project source for the new Eclipse + Android tool chain. Download here.

NOTE: This sample project is tested on Gingerbread(2.3) and should work on Froyo(2.2) and above.

Android Market on Emulator (2.2)

It’s been some time that I had tried getting the Market up and running on a 2.2 a.k.a Froyo emulator. On a previous post, I had described how to setup an emulator (1.5 and 1.6) with the Market app. Today, I got a comment on that post which points to a 2.2 image that does the same. You just have to download that image file and follow exactly the same steps to set it up.

This is the link to the system.img file. Thanks Anonymous for your contribution. I have tried this, and it works. Here’s a link (David’s Blog) that anonymous referred to. He has described all the steps to create this system image needed to put the Market app on emulator.

A few things: 
– This image will only work on a 2.2 emulator
– While creating the emulator, you should specify the cache partition size as 96 MB.

Check out the previous post for more details.

There are however a few limitations that I found with this hack.

  1. The Android Market app doesn’t seem to update itself to the new UI that we currently have on our phones.
  2. There are only a limited number of apps that show up on the Market. (No Angry Birds for example)

More security for your Android

Android has been criticized by a few of not having stricter security policies, on which a user could count on if his device is lost or stolen. Since more and more data, both personal and confidential are finding there way into our smart phones, we do need a more secure system to ensure that our data never fall into the wrong hands. Well, with Android 2.2(Froyo), your Android phones can be more secure by using the new Device Administration API.

What could this API do?
Using these APIs, the IT-Admins can install a Admin App, which enforces system wide security policies.

– Enable Passwords (Only lock screen as of now)
– Set minimum password length
– Alphanumeric password required
– Maximum password attempts
– Maximum inactivity time lock
– Lock device immediately
– Wipe the device data (to factory settings) (Remote/Local)

The key to all these features are that you have to have that admin app installed and activated as well. Also, you can have multiple admin apps that enforce multiple security policies. In situations of clash, the most strict policy will be enforced. This admin app controls how your device behaves in different situations. You can set parameters as to when and under what conditions your device should be wiped out. Say, after 20 failed password attempts, you want to wipe out the data to factory settings, the Admin app can do this using the Device Administration API.

For more information and samples on how to start with this API, visit the official documentation here.

With more and more features being packed into Android, now it is probably targeting the enterprises. Not having a way of enforcing such policies have indeed been a setback for Android to entice the Business users, but now, I can hear them come running. 😀