android自定义相册的功能实现

         做java开发的也会接触到android开发。前段时间我在做android开发时候遇到一个问题就是自定义相册。

        其实简单来说分两步:第一,先查询出本机所有图片的路径。第二,把查询出的路径放到GridView控件里面。


       接下来上具体的代码了:

      1.取图片路径:

       /**
          * 图片文件路径
         */
     private ArrayList<String> filePath = new ArrayList<String>();


       /**
         * 查询系统所有图片地址
        */
    private void serchPhoto() {
        filePath.clear();
        new Thread(new Runnable() {
            @Override
            public void run() {
                Uri mImageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                ContentResolver mContentResolver = getContentResolver();
                // 只查询jpeg和png的图片
                Cursor mCursor = mContentResolver.query(mImageUri, null,
                        MediaStore.Images.Media.MIME_TYPE + "=? or "
                                + MediaStore.Images.Media.MIME_TYPE + "=?"
                                + " or " + MediaStore.Images.Media.MIME_TYPE
                                + "=?", new String[]{"image/jpeg",
                                "image/png", "image/jpg"},
                        MediaStore.Images.Media.DATE_MODIFIED);

                Log.e("TAG", mCursor.getCount() + "===" +filePath.toString());
                while (mCursor.moveToNext()) {
                    // 获取图片的路径
                    String path = mCursor.getString(mCursor
                            .getColumnIndex(MediaStore.Images.Media.DATA));
                    Log.v("dongjie", "path==" + path);
                    filePath.add(path);
                }
                mCursor.close();
                Message msg = Message.obtain();
                //msg.obj = json;
                msg.what = 0x110;
                handler.sendMessage(msg);
            }
        }).start();
    }


  2.新建一个photoActivity   再新建一个对应的adpter适配器MoreChatAdpter  还有布局文件photo.xml

      photo.xml  :

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#F3F4F5"
    android:orientation="vertical" >

    <!--相册头部title-->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@android:color/white" >
      
      <!-- <Button
            android:id="@+id/btn_back"
            android:layout_width="22dp"
            android:layout_height="22dp"
            android:layout_centerVertical="true"
            android:layout_marginLeft="15dp"
            android:background="@drawable/btn_back" /> -->

        <TextView
            android:layout_width="62dp"
            android:layout_height="19dp"
            android:layout_centerInParent="true"
            android:layout_centerVertical="true"
            android:text="相册"
            android:textColor="#333333"
            android:textSize="16sp" />

        <Button
            android:id="@+id/btn_upload"
            android:layout_width="60dp"
            android:layout_height="24dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="10dp"
            android:background="@color/all_white"
            android:text="取消"
            android:textColor="@android:color/background_dark" />
    
    </RelativeLayout>

    <!--相册body布局-->
    <GridView
        android:id="@+id/gridview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:cacheColorHint="@android:color/transparent"
        android:clipChildren="true"
        android:gravity="center"
        android:horizontalSpacing="1dip"
        android:listSelector="@android:color/transparent"
        android:numColumns="4"
        android:stretchMode="columnWidth"
        android:verticalSpacing="1dip" />

</LinearLayout>

        

      MoreChatAdpter.java       :

 

             /**
 * 相册适配器
 * @author dongkaiming
 */
public class MoreChatAdpter extends BaseAdapter {

    public List<String> filePath;

    private LayoutInflater layoutInflater;
    private Context context;

    public MoreChatAdpter(Context context, List<String> filePath) {
        layoutInflater = LayoutInflater.from(context);
        this.filePath = filePath;
        this.context = context;
    }

    @Override
    public int getCount() {
        if (filePath == null) {
            return 1;
        }
        return filePath.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return filePath.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        HolderView holderView = null;
        if (convertView == null) {
            holderView = new HolderView();
            convertView = layoutInflater.inflate(R.layout.grid_item, null);
            holderView.photoImage = (ImageView) convertView
                    .findViewById(R.id.id_item_image);
            holderView.selectImage = (ImageView) convertView
                    .findViewById(R.id.id_select_item);
            holderView.img_camner= convertView.findViewById(R.id.img_camner);
            setHeight(holderView.photoImage);
            convertView.setTag(holderView);
        }
            holderView = (HolderView) convertView.getTag();
            if(position==0){
                holderView.img_camner.setVisibility(View.VISIBLE);
                setHeight(holderView.img_camner);
            }else{
                File file = new File(filePath.get(position-1));
                Glide.with(context).load(file).centerCrop().crossFade()
                .into(holderView.photoImage);
                holderView.img_camner.setVisibility(View.GONE);
            }
               return convertView;
    }

    /**
     * 设值每张相片的高度
     * @param view
     */
    public void setHeight(View view) {
        LayoutParams layoutParams = (LayoutParams) view.getLayoutParams();
        layoutParams.height = getLayoutHeight();
        view.setLayoutParams(layoutParams);
    }
    
    public void setWidth(View view){
        LayoutParams layoutParams = (LayoutParams) view.getLayoutParams();
        layoutParams.width = getLayoutWidth();
        view.setLayoutParams(layoutParams);
    }

    /**
     * 预设值每张相片的高度
     */
    public int getLayoutHeight() {
        int width = Tools.getScreenWidth() - 2 * 4;
        return width / 4;
    }
    
    public int getLayoutWidth(){
        int width = Tools.getScreenWidth() - 2 * 4;
        return width / 4;
    }
    
    /**
     * view mode
     * @author dongkaiming
     */
    public class HolderView {
        public ImageView photoImage;
        public ImageView selectImage;
        public View img_camner;
        
    }

}

    

           最后在photoActivity.java  里的onCreate()方法里面实例化MoreChatAdpter并传入对应的参数即可。

            path = intent.getStringArrayListExtra("path");     //path就是上面查询的图片路径。
         

            MoreChatAdpter moreChatAdpter = new MoreChatAdpter(this, path);



            好了,就是这样,请大家指教!

        

 




                 

猜你喜欢

转载自blog.csdn.net/u010074988/article/details/77261826