May 30, 2012

JPG File Chooser

We are going to modify "custom file explorer dialog" in last article to open JPG file only. The selected JPG file will be loaded in a ImageView.

JPG File Chooser


Keep no change on dialoglayout.xml, refer last article "Implement custom file explorer dialog".

Main code:
package com.AndroidCustomDialog;

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

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.MimeTypeMap;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class AndroidCustomDialogActivity extends Activity {

 Button buttonOpenDialog;
 Button buttonUp;
 TextView textFolder;
 ImageView image;
 
 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>();
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        image = (ImageView)findViewById(R.id.image);

        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;
  
    }

 @Override
 protected Dialog onCreateDialog(int id) {

  Dialog dialog = null;
  
  switch(id) {
     case CUSTOM_DIALOG_ID:
      dialog = new Dialog(AndroidCustomDialogActivity.this);
      dialog.setContentView(R.layout.dialoglayout);
      dialog.setTitle("Select JPG");
      
      dialog.setCancelable(true);
      dialog.setCanceledOnTouchOutside(true);
      
      textFolder = (TextView)dialog.findViewById(R.id.folder);

      buttonUp = (Button)dialog.findViewById(R.id.up);
      buttonUp.setOnClickListener(new OnClickListener(){

    @Override
    public void onClick(View v) {
     // TODO Auto-generated method stub
     ListDir(curFolder.getParentFile());
    }});

      //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); 
     }else {
      Toast.makeText(AndroidCustomDialogActivity.this,
        selected.toString() + " selected",
        Toast.LENGTH_LONG).show();
      dismissDialog(CUSTOM_DIALOG_ID);
      
      Bitmap bm = BitmapFactory.decodeFile(selected.getAbsolutePath());
            image.setImageBitmap(bm);

        }
     
    }});
      
         break;
     }

  return dialog;
 }

 @Override
 protected void onPrepareDialog(int id, Dialog dialog, Bundle bundle) {
  // TODO Auto-generated method stub
  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;
  textFolder.setText(f.getPath());
  
  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.getPath());
    }
   }
   
  }
       
  ArrayAdapter<String> directoryList
   = new ArrayAdapter<String>(this,
     android.R.layout.simple_list_item_1, fileList);
  dialog_ListView.setAdapter(directoryList);
 }

}

Main layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <Button
        android:id="@+id/opendialog"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Load JPG" />
 <ImageView
        android:id="@+id/image"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>


Related:
- Custom dialog to open folder, instead of individual file.


No comments:

Post a Comment

Infolinks In Text Ads