Android 使用ContentProvider扫描手机中的图片,仿微信显示本地图片效果

时间:2014-05-22 16:01:54   收藏:0   阅读:574

bubuko.com,布布扣bubuko.com,布布扣bubuko.com,布布扣

 

首先我们先看第一个界面吧,使用将手机中的图片扫描出来,然后根据图片的所在的文件夹将其分类出来,并显示所在文件夹里面的一张图片和文件夹中图片个数,我们根据界面元素(文件夹名, 文件夹图片个数,文件夹中的一张图片)使用一个实体对象ImageBean来封装这三个属性

bubuko.com,布布扣
package com.example.imagescan;

/**
 * GridView的每个item的数据对象
 * 
 * @author len
 *
 */
public class ImageBean{
    /**
     * 文件夹的第一张图片路径
     */
    private String topImagePath;
    /**
     * 文件夹名
     */
    private String folderName; 
    /**
     * 文件夹中的图片数
     */
    private int imageCounts;
    
    public String getTopImagePath() {
        return topImagePath;
    }
    public void setTopImagePath(String topImagePath) {
        this.topImagePath = topImagePath;
    }
    public String getFolderName() {
        return folderName;
    }
    public void setFolderName(String folderName) {
        this.folderName = folderName;
    }
    public int getImageCounts() {
        return imageCounts;
    }
    public void setImageCounts(int imageCounts) {
        this.imageCounts = imageCounts;
    }
    
}
bubuko.com,布布扣

接下来就是主界面的布局啦,上面的导航栏我没有加进去,只有下面的GridView,所以说主界面布局中只有一个GridView

bubuko.com,布布扣
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <GridView
        android:id="@+id/main_grid"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:listSelector="@android:color/transparent"
        android:cacheColorHint="@android:color/transparent"
        android:stretchMode="columnWidth"
        android:horizontalSpacing="20dip"
        android:gravity="center"
        android:verticalSpacing="20dip"
        android:columnWidth="90dip"
        android:numColumns="2" >
    </GridView>

</RelativeLayout>
bubuko.com,布布扣

接下来就是GridView的Item的布局,看上面的图也行你会认为他的效果是2张图片添加的效果,其实不是,后面的叠加效果只是一张背景图片而已,代码先贴上来

bubuko.com,布布扣
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <FrameLayout
        android:id="@+id/framelayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <com.example.imagescan.MyImageView
            android:id="@+id/group_image"
            android:background="@drawable/albums_bg"
            android:src="@drawable/friends_sends_pictures_no"
            android:paddingLeft="20dip"
            android:paddingRight="20dip"
            android:paddingTop="18dip"
            android:paddingBottom="30dip"
            android:scaleType="fitXY"
            android:layout_width="fill_parent"
            android:layout_height="150dip" />

        <TextView
            android:id="@+id/group_count"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/albums_icon_bg"
            android:gravity="center"
            android:layout_marginBottom="10dip"
            android:text="5"
            android:layout_gravity="bottom|center_horizontal" />
    </FrameLayout>

    <TextView
        android:id="@+id/group_title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_below="@id/framelayout"
        android:layout_centerHorizontal="true"
        android:ellipsize="end"
        android:singleLine="true"
        android:text="Camera"
        android:textSize="16sp" />

</RelativeLayout>
bubuko.com,布布扣

看到上面的布局代码,也行你已经发现了,上面使用的是自定义的MyImageView,我先不说这个自定义MyImageView的作用,待会再给大家说,我们继续看代码第一个界面的主要代码

bubuko.com,布布扣
package com.example.imagescan;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
/**
 * @blog http://blog.csdn.net/xiaanming
 * 
 * @author xiaanming
 * 
 *
 */
public class MainActivity extends Activity {
    private HashMap<String, List<String>> mGruopMap = new HashMap<String, List<String>>();
    private List<ImageBean> list = new ArrayList<ImageBean>();
    private final static int SCAN_OK = 1;
    private ProgressDialog mProgressDialog;
    private GroupAdapter adapter;
    private GridView mGroupGridView;
    
    private Handler mHandler = new Handler(){

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
            case SCAN_OK:
                //关闭进度条
                mProgressDialog.dismiss();
                
                adapter = new GroupAdapter(MainActivity.this, list = subGroupOfImage(mGruopMap), mGroupGridView);
                mGroupGridView.setAdapter(adapter);
                break;
            }
        }
        
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        mGroupGridView = (GridView) findViewById(R.id.main_grid);
        
        getImages();
        
        mGroupGridView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                List<String> childList = mGruopMap.get(list.get(position).getFolderName());
                
                Intent mIntent = new Intent(MainActivity.this, ShowImageActivity.class);
                mIntent.putStringArrayListExtra("data", (ArrayList<String>)childList);
                startActivity(mIntent);
                
            }
        });
        
    }


    /**
     * 利用ContentProvider扫描手机中的图片,此方法在运行在子线程中
     */
    private void getImages() {
        //显示进度条
        mProgressDialog = ProgressDialog.show(this, null, "正在加载...");
        
        new Thread(new Runnable() {
            
            @Override
            public void run() {
                Uri mImageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                ContentResolver mContentResolver = MainActivity.this.getContentResolver();

                //只查询jpeg和png的图片
                Cursor mCursor = mContentResolver.query(mImageUri, null,
                        MediaStore.Images.Media.MIME_TYPE + "=? or "
                                + MediaStore.Images.Media.MIME_TYPE + "=?",
                        new String[] { "image/jpeg", "image/png" }, MediaStore.Images.Media.DATE_MODIFIED);
                
                if(mCursor == null){
                    return;
                }
                
                while (mCursor.moveToNext()) {
                    //获取图片的路径
                    String path = mCursor.getString(mCursor
                            .getColumnIndex(MediaStore.Images.Media.DATA));
                    
                    //获取该图片的父路径名
                    String parentName = new File(path).getParentFile().getName();

                    
                    //根据父路径名将图片放入到mGruopMap中
                    if (!mGruopMap.containsKey(parentName)) {
                        List<String> chileList = new ArrayList<String>();
                        chileList.add(path);
                        mGruopMap.put(parentName, chileList);
                    } else {
                        mGruopMap.get(parentName).add(path);
                    }
                }
                
                //通知Handler扫描图片完成
                mHandler.sendEmptyMessage(SCAN_OK);
                mCursor.close();
            }
        }).start();
        
    }
    
    
    /**
     * 组装分组界面GridView的数据源,因为我们扫描手机的时候将图片信息放在HashMap中
     * 所以需要遍历HashMap将数据组装成List
     * 
     * @param mGruopMap
     * @return
     */
    private List<ImageBean> subGroupOfImage(HashMap<String, List<String>> mGruopMap){
        if(mGruopMap.size() == 0){
            return null;
        }
        List<ImageBean> list = new ArrayList<ImageBean>();
        
        Iterator<Map.Entry<String, List<String>>> it = mGruopMap.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, List<String>> entry = it.next();
            ImageBean mImageBean = new ImageBean();
            String key = entry.getKey();
            List<String> value = entry.getValue();
            
            mImageBean.setFolderName(key);
            mImageBean.setImageCounts(value.size());
            mImageBean.setTopImagePath(value.get(0));//获取该组的第一张图片
            
            list.add(mImageBean);
        }
        
        return list;
        
    }


}
bubuko.com,布布扣

 

Android 使用ContentProvider扫描手机中的图片,仿微信显示本地图片效果,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!