接着上次关于显示Thumbnails的问题,这里针对如何获取Android thumbnails的图片以及如何和原始图片关联,说先看一下手机内部SDcard中的Thumbnail文件(参照附件1),使用Linux下的 ls -a 命令显示隐藏文件。
提取图片和视频缩略图可以直接访问 android.provider.MediaStore.Images.Thumbnails 和android.provider.MediaStore.Video.Thumbnails这两个数据库,即可查询出来缩略图查看两个表的数据我们可以看出Thumbnails图片和原始图片之间的映射关系。
从上面两个表中可以看出,表thumbnails和Image 之间的关联是通过image_id实现的,通过会这个_id,就可以找出来thumbnails表中的图片和images表中图片的映射关系了。可以就可以获取到原始图片的位置,字段为images表中的_data字段的值。参考代码可以查看过程
package com.daisy.android.components; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; 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.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.GridView; import android.widget.ImageView; import android.widget.ListAdapter; import android.widget.SimpleAdapter; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener; import android.provider.MediaStore.Images.Thumbnails; import android.provider.MediaStore.Images.Media; /** *@author Andrew.Lee *@create 2011-6-9 下午01:17:03 *@version 1.0 *@see */ public class ThumbnailActivity extends Activity { public static String TAG = "Thumbnails"; private GridView gridView; private ArrayList<HashMap<String, String>> list; private ContentResolver cr; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main); findViews(); } private void findViews() { gridView = (GridView) findViewById(R.id.gridview); list = new ArrayList<HashMap<String, 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); String[] from = { "path" }; int[] to = { R.id.imageView }; ListAdapter adapter = new GridAdapter(this, list, R.layout.item, from, to); gridView.setAdapter(adapter); gridView.setOnItemClickListener(listener); } 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); } while (cur.moveToNext()); } } OnItemClickListener listener = new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub 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(); String path = cursor.getString(cursor .getColumnIndex(Media.DATA)); Intent intent = new Intent(ThumbnailActivity.this, ImageViewer.class); intent.putExtra("path", path); startActivity(intent); } else { Toast.makeText(ThumbnailActivity.this, "Image doesn't exist!", Toast.LENGTH_SHORT).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); // TODO Auto-generated constructor stub } // 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)); } } } }
原文: http://duguyidao.iteye.com/blog/1074012#