multimedia

Gallery intent tutorial

SDK Version: 
M3

Our goal is to start the users gallery application, allow him to select an image, and use the chosen image in our application.

API Level 3 required!

We will start the operation on a buttons onclick event, implemented as follows:

  1. private static Bitmap Image = null;
  2. private static Bitmap rotateImage = null;
  3. private ImageView imageView;
  4. private static final int GALLERY = 1;

  1. public void onClick(View v) {
  2.   imageView.setImageBitmap(null);
  3.   if (Image != null)
  4.     Image.recycle();
  5.   Intent intent = new Intent();
  6.   intent.setType("image/*");
  7.   intent.setAction(Intent.ACTION_GET_CONTENT);
  8.   startActivityForResult(Intent.createChooser(intent, "Select Picture"), GALLERY);
  9. }

In order to avoid out of memory errors, we must recycle the previous image when the user presses the button second times or after that, so the returned bitmap is stored as a member variable, so we still have a reference for it when it is needs to be recycled.

WebView with zoom controls, image from SDCARD

SDK Version: 
M3

The Problem:
When I'm trying to load an image (tried both gif and png) from my assets directory, into a webview.
In my assets folder, I have an image called myImage.gif
Here's my wrong code:

  1. WebView data = (WebView) findViewById(R.id.data)
  2. data.loadData("<IMG HEIGHT=\"42px\" WIDTH=\"42px\" SRC=\"file:///android_assets/myImage.gif\" />", "text/html",  "UTF-8");

All that happens when I try it, is that I get a blank screen with a
empty 42px/42px box.
In logcat, I see the message:
03-30 00:21:14.398: DEBUG/WebCore(214): Console: Not allowed to load
local resource: file:///android_assets/myImage.gif line: 0 source:

The Solution:
Even though, I'm going to load the image file from the filesystem (SDCARD or Phone Storage), instead of Assets folder, I've found a solution for this, and you don't even have to care about ContentProviders... (Which is also a good solution, but this is much easier & quicker in this case.)

How to send email from your application

SDK Version: 
M3

Today we'll create an easy email sender application.
First of all we need to create a layout to set the address, the subject and email body box.

  1. <?xml version="1.0" encoding="utf-8"?><LinearLayout android:id="@
  2.  
  3. +id/LinearLayout01" android:layout_width="fill_parent"
  4.  
  5. android:layout_height="fill_parent"
  6.  
  7. xmlns:android="http://schemas.android.com/apk/res/android"
  8.  
  9. android:orientation="vertical"><LinearLayout android:id="@+id/LinearLayout02";
  10.  
  11. android:layout_width="wrap_content" android:layout_height="wrap_content"
  12.  
  13. android:orientation="horizontal"><EditText android:layout_width="wrap_content"
  14.  
  15. android:layout_height="wrap_content" android:width="170dip" android:id="@
  16.  
  17. +id/emailaddress"></EditText><TextView android:layout_width="wrap_content"
  18.  
  19. android:layout_height="wrap_content" android:id="@+id/emailaddress"
  20.  
  21. android:text="Email address"></TextView>
  22. </LinearLayout>
  23.  
  24.  
  25. <LinearLayout android:id="@+id/LinearLayout03";

How to play video and audio on Android Part 2.

SDK Version: 
M3

In this part, I'm going to focus on MediaPlayer. I'm going to show you, how to stop it properly, and how to create a media player more easily.
The default behavior is the same, with every activity, after onPause and onStop, if the activity is not destroyed, it will continue to run in the background, until it is killed so the system can free up the memory it is using. So until the activity is destroyed, the mediaplayer will continue to occupy space in the phones memory.

How to play video and audio on Android

SDK Version: 
M3

There is more than one way, to play media files on an Android phone, let me show you two of them.

picture

Audio:
MediaPlayer is the easier way, if you just want to play an audio file in the background, somewhere in an appliaction. There are no ui controls here, but of course you can use MediaPlayer.stop(), play(), seekTo() ,etc. Just bind the needed functions to a button, gesture, or event. As you can see, it also throws a lot of exceptions, which you need to catch.

  1.  public void audioPlayer(String path, String fileName){
  2.     //set up MediaPlayer    
  3.     MediaPlayer mp = new MediaPlayer();
  4.  
  5.     try {
  6.         mp.setDataSource(path+"/"+fileName);
  7.     } catch (IllegalArgumentException e) {
  8.         // TODO Auto-generated catch block
  9.         e.printStackTrace();
  10.     } catch (IllegalStateException e) {

Store images/files in database

SDK Version: 
M3

Hoever the practice is to store them normally and save the access route (Uri), sometimes it can be handy to store files/images completely in database.
In sqlite database there are only a few data types, so its easy to choose: files can be stored in a text as a ByteArray.

Lets see a sample code, where we download an image from the Internet then store it in the local database:

  1. //where we want to download it from
  2. URL url = new URL(IMAGE_URL);  //http://example.com/image.jpg
  3. //open the connection
  4. URLConnection ucon = url.openConnection();
  5. //buffer the download
  6. InputStream is = ucon.getInputStream();
  7. ByteArrayBuffer baf = new ByteArrayBuffer(128);
  8. //get the bytes one by one
  9. int current = 0;
  10. while ((current = bis.read()) != -1) {
  11.         baf.append((byte) current);
  12. }
  13.  
  14. //store the data as a ByteArray
  15. //db is a SQLiteDatabase object
  16. ContentValues dataToInsert = new ContentValues();                          

MusicDroid - Audio Player Part III

SDK Version: 
M3

I part 3 of the MusicDroid tutorial we create a simple controls interface that will be launched by our playlist when you play a song. The controls interface is a translucent Activity that simply has 4 images, one for each direction on the D-Pad. When you hit a direction on the pad it will send a command to the service and play a small animation.

This tutorial utilizes the work done in parts 1 and 2, though you do not have to be familiar with those parts if you just want some examples of animation or transparency.

MusicDroid - Audio Player Part II

SDK Version: 
M3

In part 2 of the MusicDroid media player tutorial series, we will move the MediaPlayer object into a remote service so that you can use the rest of the phone while playing your music. We will setup this service so that it will be simple to add the controls in part 3 for Pause, Stop, Skip forward, Skip back, etc. This service will also provide a marquee notification each time a new song starts, and a notification icon for play/pause status.

MusicDroid - Audio Player Part I

SDK Version: 
M3

In part 1 of this 4 part series, we will setup a very basic music player that will list all songs on the SD card and allow the user to select a song to play. Over the course of this series we will build a fully functional MP3 player with animated controls and IDE3 tag support.

Syndicate content