Posts Tagged ‘Android’

Posts that contains tutorials and information about Google’s OS, Android.

Android: changing the ringer volume

Click here to read Android: changing the ringer volume

Another Android tutorial, this time, explaining how to change the different sound volumes of the Android system, such as the ringer, music and notification loudness. The source code featured below is available for download at the end of the post.

Using a seek bar to control audio’s volume in Android is a lot like changing the screen brightness with a seek bar. However, instead of using a ContentResolver to obtain the service that controls the audio, Android has a class that makes everything easier, named AudioManager. So, the example here will basically obtain a reference to the AudioManager and will use it to get the current ringer volume and use it to set the progress of a seek bar. When changed, the seek bar progress will set the value of the sound volume. Here is the code: (more…)

Android: Loading files from the Assets and Raw folders

Click here to read Android: Loading files from the Assets and Raw folders

This tutorial will explain how to load files from the res/raw and the Assets folder using a String to specify the file name. Yes, there are a lot of tutorials on this subject, but they all use the automatically generated integer IDs from the R class as inputs and not many of them even mention the possibility of loading files from the Assets folder. As a result of depending on the ID, the file reference must be known beforehand.

Instead, the code featured in this post will explain how to find the reference to the file and then load it at runtime based solely on its name. This means that the reference ID and the file don’t even have to exist in the first place, and can be acquired at run time.

(more…)

Android: take a picture without displaying a preview

Click here to read Android: take a picture without displaying a preview

Accessing hardware functionality when programming for an Android device is generally quite straightforward. The same can be said about writing an Activity that takes a picture, but Android requires a preview of what the camera will capture to be displayed prior to capturing an image. This post explains how to “cheat” this requirement imposed by the OS, and how to write an application that takes a picture and displays it.

An Eclipse project with all the code explained here is available for download at the end of the post.

Before going into the Activity code, the interface layout (the main.xml file) must be edited to add a Surface View and an Image View to the interface. To add an element, just drag and drop it from the list inside your layout, like this: (more…)

Android: Acessing the gyroscope sensor for simple applications

Click here to read Android: Acessing the gyroscope sensor for simple applications

This post explains how to get values from the gyroscope (or any sensor that returns the device’s relative angle) to create simple application. The reason why I’m stating ‘for simple applications’ is because the code featured here is already deprecated. I’m just explaining how to do it because it still works, and it’s very clean and short to explain, as opposed to the new method, which is much more accurate but more complex to implement.

Still, it’s possible to use it for simple applications, although, if the application requires accuracy from the sensors, such as augmented reality applications or even games, it’s recommended to use the getRotationMatrix() method from the Sensor Manger class instead.

With that said, the following code just prints the rotation values from the gyroscope on the screen: (more…)

Android: How to return RGB values from an image file

Click here to read Android: How to return RGB values from an image file

Surprisingly, it’s quite easy to get the RGB value from an image file in Android. It’s certainly a lot easier than retrieving the pixel values from the Camera Preview. All that is required is to load the image file into a Bitmap object and them call the getPixel() method from the loaded bitmap.

It’s also possible to call this method to create an array of alpha and RGB values (ARGB) without calling the copyPixelsToBuffer() method, avoiding the use of buffers in Android which make things more complicated than it should be. As an example, the following code prints the color values of the pixels from the 4 corners of the image in LogCat and creates a RGBA array that stores each pixel color value:

(more…)