Saturday, July 14, 2012

Implement Android Gallery widget with scale-down bitmap

In the exercise of "Android Gallery widget", the bitmap are loaded in Gallery widget without re-size. If you load with big picture, error of java.lang.OutOfMemoryError will be thrown.

Last post "Scale bitmap Efficiently" explain how to scale-down bitmap. It will be apply in the Gallery.

Android Gallery widget with scale-down bitmap

package com.example.androidgallery;

import java.util.ArrayList;

import android.os.Bundle;
import android.os.Environment;
import android.content.Context;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends Activity {

    public class GalleryBaseAdapter extends BaseAdapter {

     ArrayList<String> GalleryFileList;
     Context context;
     GalleryBaseAdapter(Context cont){
      context = cont;
      GalleryFileList = new ArrayList<String>();    
  public int getCount() {
   return GalleryFileList.size();

  public Object getItem(int position) {
   return GalleryFileList.get(position);

  public long getItemId(int position) {
   // TODO Auto-generated method stub
   return position;

  public View getView(int position, View convertView, ViewGroup parent) {
   //Bitmap bm = BitmapFactory.decodeFile(GalleryFileList.get(position));
   Bitmap bm = decodeSampledBitmapFromUri(GalleryFileList.get(position), 200, 200);
   LinearLayout layout = new LinearLayout(context);
   layout.setLayoutParams(new Gallery.LayoutParams(250, 250));

   ImageView imageView = new ImageView(context);
   imageView.setLayoutParams(new Gallery.LayoutParams(200, 200));
   return layout;

  public void add(String newitem){

    GalleryBaseAdapter myGalleryBaseAdapter;
    Gallery myPhotoGallery;
    public void onCreate(Bundle savedInstanceState) {
        myPhotoGallery = (Gallery)findViewById(;
        myGalleryBaseAdapter = new GalleryBaseAdapter(this);
        String ExternalStorageDirectoryPath = Environment
        String targetPath = ExternalStorageDirectoryPath + "/test/";
        Toast.makeText(getApplicationContext(), targetPath, Toast.LENGTH_LONG).show();
        File targetDirector = new File(targetPath);
        File[] files = targetDirector.listFiles();
        for (File file : files){

    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(, menu);
        return true;
    public Bitmap decodeSampledBitmapFromUri(String path, int reqWidth, int reqHeight) {
     Bitmap bm = null;
     // First decode with inJustDecodeBounds=true to check dimensions
     final BitmapFactory.Options options = new BitmapFactory.Options();
     options.inJustDecodeBounds = true;
     BitmapFactory.decodeFile(path, options);
     // Calculate inSampleSize
     options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
     // Decode bitmap with inSampleSize set
     options.inJustDecodeBounds = false;
     bm = BitmapFactory.decodeFile(path, options); 
     return bm; 
    public int calculateInSampleSize(
      BitmapFactory.Options options, int reqWidth, int reqHeight) {
     // Raw height and width of image
     final int height = options.outHeight;
     final int width = options.outWidth;
     int inSampleSize = 1;
     if (height > reqHeight || width > reqWidth) {
      if (width > height) {
       inSampleSize = Math.round((float)height / (float)reqHeight);  
      } else {
       inSampleSize = Math.round((float)width / (float)reqWidth);  
     return inSampleSize;  


The layout refer "Android Gallery widget".

Download the files.

- Caching Bitmaps with LruCache

No comments: