Jun 21, 2012

Implement AdapterView.OnItemClickListener for Gallery

Modify last article "Get Exif of jpg using ExifInterface, and thumbnail inside the JPEG" to implement AdapterView.OnItemClickListener for the Gallery. When user touch on any item, the callback method onItemClick (AdapterView<?> parent, View view, int position, long id) will be called. To access the data associated with the selected item, call parent.getItemAtPosition(position).

Implement AdapterView.OnItemClickListener for Gallery


package com.AndroidGallery;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.R.color;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.MimeTypeMap;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class AndroidGalleryActivity extends Activity {
 
 Gallery photoBar;
 
 Button buttonOpenDialog;
 Button buttonUp, buttonSelectFolder;
 TextView jpgList;
   
 String KEY_TEXTPSS = "TEXTPSS";
 static final int CUSTOM_DIALOG_ID = 0;
 
 ListView dialog_ListView;
 
 File root;
 File curFolder;
 
 private List<String> fileList = new ArrayList<String>();
 
 class PhotoItem{
  
  ExifInterface exifInterface = null;
  boolean exifHasThumbnail = false;
  
  String bitmapImageSrc;
  Bitmap bitmapImage = null;
  
  final static int itemWidth = 150;
  final static int itemHeight = 150;
  
  Handler handler;
  
  public PhotoItem(String src){
   bitmapImageSrc = src;
   StratBackgroundProcess();
   
   bitmapImage = BitmapFactory.decodeResource(getResources(), android.R.drawable.ic_menu_gallery);
  }
  
  public Bitmap getImage(){
   return bitmapImage; 
  }
  
  private void runResize(){
   BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
   bmpFactoryOptions.inJustDecodeBounds = true;

   Bitmap bitmap = BitmapFactory.decodeFile(bitmapImageSrc, bmpFactoryOptions);
   
   int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)itemHeight);
   int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)itemWidth);
   
   if (heightRatio > 1 || widthRatio > 1)
   {
    if (heightRatio > widthRatio){
     bmpFactoryOptions.inSampleSize = heightRatio; 
    } else {
     bmpFactoryOptions.inSampleSize = widthRatio;  
    }  
   }
             
   bmpFactoryOptions.inJustDecodeBounds = false;
   
   bitmapImage = BitmapFactory.decodeFile(bitmapImageSrc, bmpFactoryOptions);

  }
  
  private void getExif(){
   
   try {
    exifInterface = new ExifInterface(bitmapImageSrc);
    if(exifInterface.hasThumbnail()){
     byte[] thumbnail =  exifInterface.getThumbnail();
     bitmapImage = BitmapFactory.decodeByteArray(thumbnail, 0, thumbnail.length);
     exifHasThumbnail = true;
    }
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }

  }
  
  private void StratBackgroundProcess(){
   Runnable runnable = new Runnable(){

    @Override
    public void run() {

     getExif();
     if(!exifHasThumbnail){
      runResize();
     }
     
     handler.post(new Runnable(){

      @Override
      public void run() {
       myPhotoBarAdapter.notifyDataSetChanged();
      }});
     
    }
    
   };
   
   handler = new Handler();
   new Thread(runnable).start();
  }
  
 }
 
 PhotoBarAdapter myPhotoBarAdapter;
 
 public class PhotoBarAdapter extends BaseAdapter {
  
  Context context;
  ArrayList<PhotoItem> arrayPhotoItem;

  PhotoBarAdapter(Context c){
      context = c;
      arrayPhotoItem = new ArrayList<PhotoItem>();    
  }
  
  public void clear(){
   arrayPhotoItem.clear();
  }
  
  public void addPhotoItem(PhotoItem item){
   arrayPhotoItem.add(item);    
  }
  
  @Override
  public int getCount() {
   return arrayPhotoItem.size();
  }

  @Override
  public Object getItem(int position) {
   return arrayPhotoItem.get(position);
  }

  @Override
  public long getItemId(int position) {
   return position;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
   
   LinearLayout viewLayout = new LinearLayout(context);
   viewLayout.setLayoutParams(new Gallery.LayoutParams(200, 200));
   viewLayout.setGravity(Gravity.CENTER);
   viewLayout.setBackgroundColor(color.background_light);
   
   ImageView imageView;
   imageView = new ImageView(context);
   imageView.setLayoutParams(new Gallery.LayoutParams(150, 150));
   imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
   imageView.setImageBitmap(arrayPhotoItem.get(position).getImage());
   //return imageView;
   
   viewLayout.addView(imageView);
   return viewLayout;
  }
  
 }
 
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  
  photoBar = (Gallery)findViewById(R.id.photobar);
  myPhotoBarAdapter = new PhotoBarAdapter(this);
  photoBar.setAdapter(myPhotoBarAdapter);
   
  jpgList = (TextView)findViewById(R.id.jpglist);
   
  buttonOpenDialog = (Button)findViewById(R.id.opendialog);
  buttonOpenDialog.setOnClickListener(new Button.OnClickListener(){
   @Override
   public void onClick(View arg0) {
    showDialog(CUSTOM_DIALOG_ID); 
   }});
   
  root = new File(Environment
    .getExternalStorageDirectory()
    .getAbsolutePath());
   
  curFolder = root;
  //ListJpgInFolder(curFolder);
  preparePhotoBarInFolder(curFolder);
  
  //Gallery onClickListener
  photoBar.setOnItemClickListener(photoBarOnItemClickListener);
   
 }
 
 OnItemClickListener photoBarOnItemClickListener
 = new OnItemClickListener(){

  @Override
  public void onItemClick(AdapterView<?> parent, View view, int position,
    long id) {
   
   String itemInfo = ((PhotoItem)(parent.getItemAtPosition(position))).bitmapImageSrc;
   
   Toast.makeText(AndroidGalleryActivity.this, 
     "Clicked item:\n" + itemInfo, 
     Toast.LENGTH_LONG).show();
   
  }
  
 };

  
 @Override
 protected Dialog onCreateDialog(int id) {
   
  Dialog dialog = null;
   
  switch(id) {
  case CUSTOM_DIALOG_ID:
   dialog = new Dialog(AndroidGalleryActivity.this);
   dialog.setContentView(R.layout.dialoglayout);
   dialog.setTitle("Select JPG");
    
   dialog.setCancelable(true);
   dialog.setCanceledOnTouchOutside(true);
    
   buttonUp = (Button)dialog.findViewById(R.id.up);
   buttonUp.setOnClickListener(new OnClickListener(){
     
    @Override
    public void onClick(View v) {

     ListDir(curFolder.getParentFile()); 
    }});
   
   buttonSelectFolder = (Button)dialog.findViewById(R.id.selectfolder);
   buttonSelectFolder.setOnClickListener(new OnClickListener(){
    
   @Override
   public void onClick(View v) {

    Toast.makeText(AndroidGalleryActivity.this,
      curFolder + " selected",
      Toast.LENGTH_LONG).show();
    dismissDialog(CUSTOM_DIALOG_ID);
    
    //ListJpgInFolder(curFolder);
    preparePhotoBarInFolder(curFolder);

   }});
   
    
   //Prepare ListView in dialog
   dialog_ListView = (ListView)dialog.findViewById(R.id.dialoglist);
    
   dialog_ListView.setOnItemClickListener(new OnItemClickListener(){
     
    @Override
    public void onItemClick(AdapterView<?> parent, View view,
      int position, long id) {
     File selected = new File(fileList.get(position));
     if(selected.isDirectory()){
      ListDir(selected);  
     } 
    }});
        
   break;
  }
  return dialog; 
 }
  
 @Override
 protected void onPrepareDialog(int id, Dialog dialog, Bundle bundle) {

  super.onPrepareDialog(id, dialog, bundle);
   
  switch(id) {
  case CUSTOM_DIALOG_ID:
   ListDir(curFolder);
   
   break; 
  } 
 }
  
 void ListDir(File f){
   
  if(f.equals(root)){
   buttonUp.setEnabled(false); 
  }else{
   buttonUp.setEnabled(true); 
  }
   
  curFolder = f;
  buttonSelectFolder.setText("Select Folder " + curFolder);
   
  File[] files = f.listFiles();
  fileList.clear();
  for (File file : files){
   if(file.isDirectory()){
    fileList.add(file.getPath()); 
   }else{
    Uri selectedUri = Uri.fromFile(file);
    String fileExtension
     = MimeTypeMap.getFileExtensionFromUrl(selectedUri.toString());
    if(fileExtension.equalsIgnoreCase("jpg")){
     fileList.add(file.getName()); 
    } 
   } 
  }
   
  ArrayAdapter<String> directoryList
   = new ArrayAdapter<String>(this,
     android.R.layout.simple_list_item_1, fileList);
  dialog_ListView.setAdapter(directoryList); 
 }
 
 private void preparePhotoBarInFolder(File folder){
  
  String jpgs = "JPG fiels in folder " + folder.getAbsolutePath() + "\n\n";
  
  File[] files = folder.listFiles();
  
  myPhotoBarAdapter.clear();
  
  for (File file : files){
   if(!file.isDirectory()){
    Uri selectedUri = Uri.fromFile(file);
    String fileExtension
     = MimeTypeMap.getFileExtensionFromUrl(selectedUri.toString());
    if(fileExtension.equalsIgnoreCase("jpg")){
     jpgs += file.getAbsolutePath() + "\n";
     
     PhotoItem pItem = new PhotoItem(file.getAbsolutePath());
     myPhotoBarAdapter.addPhotoItem(pItem);
     
     //Forcer myPhotoBarAdapter update when each photos added.
     myPhotoBarAdapter.notifyDataSetChanged();
    }  
   }  
  }
  
  //Forcer myPhotoBarAdapter update again even without photo.
  myPhotoBarAdapter.notifyDataSetChanged();
  
  jpgList.setText(jpgs); 
 }
  
}


Next:
- Read Exif info


No comments:

Post a Comment

Infolinks In Text Ads