Thursday, May 5, 2011

ThumbnailUtils

android.media.ThumbnailUtils, was introduced since API Level 8, Android 2.2. It's provide thumbnail generation routines for media provider.

AndroidThumbnailUtils

Copy a bitmap file to /res/drawable/ folder, bitmap_1.png in my exercise.

Modify main.xml layout to have a ImageView to display the generated thumbnail.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<ImageView
android:id="@+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>


Java Code:
package com.exercise.AndroidThumbnailUtils;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.ThumbnailUtils;
import android.os.Bundle;
import android.widget.ImageView;

public class AndroidThumbnailUtils extends Activity {

private final int Thumbnail_WIDTH = 100;
private final int Thumbnail_HEIGHT = 100;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView thumbnail = (ImageView)findViewById(R.id.thumbnail);

Bitmap bmSource
= BitmapFactory.decodeResource(getResources(),
R.drawable.bitmap_1);

Bitmap bmThumbnail
= ThumbnailUtils.extractThumbnail(bmSource,
Thumbnail_WIDTH, Thumbnail_HEIGHT);

thumbnail.setImageBitmap(bmThumbnail);
}
}


Download the files.

Related article:
- Create thumbnail for a video using ThumbnailUtils
- Display Video thumbnail in ListView

No comments:

Post a Comment