Easy Video Chooser Library for Android

After I wrote the “Easy Image Chooser library for Android“, I thought I could easily extend this library to handle other things as well. For now, I have implemented the option of choosing/recording a video. And in the future, I am planning to extend this library for choosing other types of files.

Choosing videos, is again very similar to how the images are handled. Using this library, you would be also able to choose videos that reside in your Picasa albums on your phone. And this should work for all OSes and devices.

After processing, the library would return a ChosenVideo object, which would contain the original path to the video file, and 2 differently-sized thumbnails which you can directly use as a preview.

Here is how to use this library.

  • For capturing a video using the camera

videoChooserManager = new VideoChooserManager(this, ChooserType.REQUEST_CAPTURE_VIDEO);
videoChooserManager.setVideoChooserListener(this);
videoChooserManager.choose();

  •  For choosing a video from the gallery

videoChooserManager = new VideoChooserManager(this, ChooserType.REQUEST_PICK_VIDEO);
videoChooserManager.setVideoChooserListener(this);
videoChooserManager.choose();

  • On Activity Result, write this

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK &&
(requestCode == ChooserType.REQUEST_PICK_VIDEO ||
requestCode == ChooserType.REQUEST_CAPTURE_VIDEO)) {
videoChooserManager.submit(requestCode, data);
}

@Override
public void onVideoChosen(final ChosenVideo video) {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (video != null) {
// Use the video
// video.getFilePathOriginal();
// video.getFileThumbnail();
// video.getFileThumbnailSmall();
}
}
});
}

@Override
public void onError(final String reason) {
runOnUiThread(new Runnable() {
@Override
public void run() {
// Show error message
}
});
}

Here’s the link to the Github project. Do let me know your thoughts and feedbacks, and also any bugs if you come across. I will try to fix them as soon as possible.

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.
Usage:
1. For choosing an image from gallery

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

2. For capturing a picture using your camera

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

3. On Activity result, do this:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK &&
(requestCode == ChooserType.
REQUEST_PICK_PICTURE||
requestCode == ChooserType.
REQUEST_CAPTURE_PICTURE)) {
imageChooserManager.submit(requestCode, data);
}
}

4. Implement the ImageChooserListener interface and override these methods:

@Override
public void onImageChosen(final ChosenImage image) {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (image != null) {
// Use the image
// image.getFilePathOriginal();
// image.getFileThumbnail();
// image.getFileThumbnailSmall();
}
}
});
}
@Override
public void onError(final String reason) {
runOnUiThread(new Runnable() {
@Override
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.

Google Maps API v2 on Android

The Splash Screen

I don’t even remember when was the last time I did something with MapView on Android. I was aware of the new version 2 of the Google Maps API, but never got a chance to work on it until today.

There were initial hitches to setup the project. But, once the first steps were done, it was quite easy to build up a simple app.

I want to list down the first steps of the process, and then move on to the sample application.

Step 1: Getting the API key.

  1. Go to Google API Console
  2. Go to Services link, and enable Google Maps Android API v2. (Don’t confuse this with Google Maps API v2. I got confused, and was wondering what was I doing wrong when the maps failed to load)
  3. After enabling the service, go to the API Access link.
  4. In the Simple API access section, click on “Create New Android Key”.
  5. Enter your keystore’s SHA1 fingerprint along with your application’s package name separated by a semi-colon in the dialog and click on “Save”.
  6. Your API should be visible now on the same page.
Step 2: Setting up the project.
  1. You need to find the library project for the Google Maps API v2. It should be available in your SDK directory. If not, open up the Android SDK manager and download the same by selecting Google Play Services in the Extras section.
  2. You would find the required library project in the following location of the SDK folder. (..android-sdksextrasgooglegoogle_play_serviceslibproject)
  3. My suggestion would be to make a copy of this project somewhere else, and then import the project into eclipse.
  4. Once imported into Eclipse, use this as a library project for your application.
Step 3: Setting up the manifest file.
  1. Your manifest file should look like this. AndroidManifest.xml
  2. You can change the package names for the permissions to use your own package name.
  3. Now, replace your API key with the place-holder API Key in the manifest.
Step 4: MapFragment and more code…
  1. To show a map, I used the MapFragment in the activity’s layout.
That’s it. These are the broad steps to configure the new version of Google Maps for your Android application.
Your Places
Add a new place
What this application does?
  1. On opening the application, you would see a splash screen, and it would try to get your current location.
  2. Once it gets your location, it would switch to the map view, and display your current location with a marker.
  3. Tapping on an unmarked area of the app, you would see a dialog, through which you can add a new place.
  4. After saving the new place by providing a name and a description, this place would be saved into the database and you would be able to see the newly added place on the map.
  5. Tapping on an already visible marker will show you the details of that place.
  6. The next time you open the application, all the previously stored places would appear on the map along with your current location.
Suggested optimizations
  1. While loading the points from the database, it would be nice if we can only load the places which are within the currently visible area of your app.
  2. When the map is re-sized or moved, we could re-query the database for the fresh list of places and update the markers based on the new visible area.
Problems:
  1. The VisibleRegion API, currently, doesn’t work. Need to think of a workaround for this.

You can find the full source-code here at the github respository: android-map101-v2

Easier Bug Reporting on 4.2

With the recent release of an updated Jelly Bean version, i.e, 4.2, there have been quite some new things to awe you.

These are two things that could probably make a developers life easier.

  • Take bug report — immediately takes a screen shot and dumps device state information to local file storage, then attaches them to a new outgoing email message.
  • Power menu bug reports — Adds a new option to the device power menu and quick settings to take a bug report (see above).
Remember, while you were testing your app on a bus, or you were away from your desktop/laptop and got a dreaded crash!! You, so eagerly want to have a look at the logcat, or even save the logcat output for later investigation. And most of the times, I don’t have SendLog, which I could fire up, and send me the logs.
With 4.2, it’s already built-in to your phone. Here are a few screenshots, that give you an idea.
Enable this option from the Settings Page.

Hold the power button, to see the option to capture a “Bug Report”
Happy Coding…

Check orientation of images/captures

A lot of times, you would need your app to either pick an image from the gallery or use the device’s camera for capturing a picture that your app could use. I have seen a lot of apps, do it plain wrong. Especially, the orientation of the images.

The default gallery app, reads the orientation properly, and displays the images/thumbnails properly. So, our apps can also handle images in various orientations properly. And the good news is, it’s very easy to handle.

There’s a class called ExifInterface. Most of the times, when you have a similar situation, you would almost never want a full-scaled image to be shown in your app. Most often, we use a thumbnail view for the purpose. The following code would get you a re-sized bitmap, from your original file.

Say for example, we have this path to the actual image file. imagePath


1. Create a Bitmap from the file

Bitmap b = BitmapFactory.decodeFile(imagePath);

2. Resize the Bitmap by scaling it to appropriate level

int width = b.getWidth();
int height = b.getHeight();
int newWidth = 150;
int newHeight = 150;
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
// Bitmap resizedBitmap = Bitmap.createBitmap(b, 0, 0, width, height, matrix, true);
// resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 70, out);

3. Handle orientation of the image

ExifInterface exif = new ExifInterface(imagePath);
String orientation = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
if (orientation.equals(ExifInterface.ORIENTATION_NORMAL)) {
        // Do nothing. The original image is fine.
} else if (orientation.equals(ExifInterface.ORIENTATION_ROTATE_90+””)) {
        matrix.postRotate(90);
} else if (orientation.equals(ExifInterface.ORIENTATION_ROTATE_180+””)) {
        matrix.postRotate(180);
} else if (orientation.equals(ExifInterface.ORIENTATION_ROTATE_270+””)) {
        matrix.postRotate(270);
}

4. Save the new bitmap 

out = new FileOutputStream(new File(“some output file path”));
Bitmap resizedBitmap = Bitmap.createBitmap(b, 0, 0, width, height, matrix, true);
resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 70, out);

Now your output file would be an image that is resized and handled properly for orientation of the images. You could directly use the “resized” bitmap, but I prefere files.