andriod 缩略图Thumbnails学习

/**
 * @(#)MainActivity.java
 * 
 * Copyright XXX.All rights reserved.
 * This software is the XXX system. 
 *
 * @Version: XXX
 * @JDK: jdk 1.6.0.XXX
 * @Module: androidThumbnail
 */ 
 /*- 		History
 **********************************************
 *  ID      DATE           PERSON       REASON
 *  1     2012-7-11    WuChaoWen    Created
 **********************************************
 */

package com.example.androidthumbnail;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import android.R.bool;
import android.R.integer;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.MediaStore.Images.Media;
import android.provider.MediaStore.Images.Thumbnails;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
import android.widget.Toast;

/**
 * Class description goes here.
 * @author WuChaoWen
 * @since 2012-7-11
 */
public class MainActivity extends Activity {
	 public static String TAG = "Thumbnails";   
	 public static boolean flag = true;
	 ImageView ivImageShow;
	    private GridView gridView;   
	    private ArrayList<HashMap<String, String>> list;   
	    private ContentResolver cr;  
	    private ArrayList<String> fileList ;
	    public void onCreate(Bundle savedInstanceState) {
	        requestWindowFeature(Window.FEATURE_NO_TITLE);//去除标题
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.main);  
	        findViews();
	}
	    
	    
	    @SuppressLint("ParserError")
		private void findViews() {   
	    	ivImageShow = (ImageView)this.findViewById(R.id.ivImageShow);
	        gridView = (GridView) findViewById(R.id.gridview);   
	        list = new ArrayList<HashMap<String, String>>();   
	        fileList = new ArrayList<String>();
	        cr = getContentResolver();   
	        String[] projection = { Thumbnails._ID, Thumbnails.IMAGE_ID,   
	                Thumbnails.DATA};   //所要查询的字段
	        Cursor cursor = cr.query(Thumbnails.EXTERNAL_CONTENT_URI, projection,   
	                null, null, null);   
	        
	        getColumnData(cursor);   
	    //    changeFile();
	        String[] from = { "path" };   
	        int[] to = { R.id.ItemImage };   
	        ListAdapter adapter = new GridAdapter(this, list, R.layout.item, from,to);   
	        gridView.setAdapter(adapter);   
	        gridView.setOnItemClickListener(listener);   
	      
	    }   
	    
	    
	    public Uri[] uriArray;
	    private void getColumnData(Cursor cur) {   
	        if (cur.moveToFirst()) {   
	            int _id;   
	            int image_id;   
	            String image_path;   
	            int _idColumn = cur.getColumnIndex(Thumbnails._ID);   
	            int image_idColumn = cur.getColumnIndex(Thumbnails.IMAGE_ID);   
	            int dataColumn = cur.getColumnIndex(Thumbnails.DATA);   
	            do {   
	                // Get the field values   
	                _id = cur.getInt(_idColumn);   
	                image_id = cur.getInt(image_idColumn);   
	                image_path = cur.getString(dataColumn);   
	                // Do something with the values.   
	                Log.i(TAG, _id + " image_id:" + image_id + " path:"  
	                        + image_path + "---");   
	                HashMap hash = new HashMap();   
	                hash.put("image_id", image_id + "");   
	                hash.put("path", image_path);   
	                list.add(hash);   
	  
	                //根据image_id获取真实路径
	               String path = getFileByImageId(image_id);
	              fileList.add(path);
	            } while (cur.moveToNext());   
	  
	        }   
	    }  
	    
	    
	    OnItemClickListener listener = new OnItemClickListener() {   
	    	  
	        @Override  
	        public void onItemClick(AdapterView<?> parent, View view, int position,   
	                long id) {   
	            // TODO Auto-generated method stub   
	        //1.获取选定图片的path
	        	String path = "";
	            String image_id = list.get(position).get("image_id");   
	            Log.i(TAG, "---(^o^)----" + image_id);   
	            String[] projection = { Media._ID, Media.DATA };   
	            Cursor cursor = cr.query(Media.EXTERNAL_CONTENT_URI, projection,   
	                    Media._ID + "=" + image_id, null, null);   
	            if (cursor != null) {   
	                cursor.moveToFirst();   
	                 path = cursor.getString(cursor   
	                        .getColumnIndex(Media.DATA));   
	                 Log.i("path:", path);
	            } else {   
	                Toast.makeText(MainActivity.this, "Image doesn't exist!",   
	                        Toast.LENGTH_SHORT).show();   
	            }   
	            
	            
	            //2.以一个对话框来显示图片
	            
	        	// TODO Auto-generated method stub
	    		final Dialog dialog = new Dialog(MainActivity.this);
	    		// 以对话框形式显示图片
				dialog.setContentView(R.layout.image_show);
				dialog.setTitle("图片显示");

				ImageView ivImageShow = (ImageView) dialog.findViewById(R.id.ivImageShow);
				Button btnClose = (Button) dialog.findViewById(R.id.btnClose);

				btnClose.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						dialog.dismiss();
					}
					
				});
				
				Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI.buildUpon().
						  appendPath(Long.toString(id)).build();
				FileUtil file = new FileUtil();
				ContentResolver resolver = getContentResolver();
				InputStream inputStream = null;  


				// 从Uri中读取图片资源
				try {
					inputStream = new FileInputStream(path);  
					Bitmap bitmap = BitmapFactory.decodeStream(inputStream);  
					ivImageShow.setImageBitmap(bitmap);
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				}

				dialog.show();
	  
	        }   
	    };   
	    
	    
	    
	    class GridAdapter extends SimpleAdapter {   
	    	  
	        public GridAdapter(Context context,   
	                List<? extends Map<String, ?>> data, int resource,   
	                String[] from, int[] to) {   
	            super(context, data, resource, from, to);   
	           
	        }   
	  
	        // set the imageView using the path of image   
	        public void setViewImage(ImageView v, String value) {   
	            try {   
	                Bitmap bitmap = BitmapFactory.decodeFile(value);   
	                Bitmap newBit = Bitmap   
	                        .createScaledBitmap(bitmap, 100, 80, true);   
	                v.setImageBitmap(newBit);   
	            } catch (NumberFormatException nfe) {   
	                v.setImageURI(Uri.parse(value));   
	            }   
	        }   
	    }   
	    
	    
	     
	    //整理缩略图的uri转成file并归类
	    public String getFileByImageId(int image_id){
	    	
	    	String path = "";
           
            Log.i(TAG, "---(^o^)----" + image_id);   
            String[] projection = { Media._ID, Media.DATA };   
            Cursor cursor2 = cr.query(Media.EXTERNAL_CONTENT_URI, projection,   
                    Media._ID + "=" + image_id, null, null);   
            if (cursor2 != null) {   
                cursor2.moveToFirst();   
                 path = cursor2.getString(cursor2   
                        .getColumnIndex(Media.DATA));   
                 Log.i("path:", path);
            } else {   
                Toast.makeText(MainActivity.this, "Image doesn't exist!",   
                        Toast.LENGTH_SHORT).show();   
            }     
	    	
	    	return path;
	    }
}

  

posted @ 2012-07-13 09:12  吴超文  阅读(823)  评论(0编辑  收藏  举报