码迷,mamicode.com
首页 > 移动开发 > 详细

android 显示目录下图片

时间:2016-04-17 16:03:20      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:

知识点

1. 网格视图

2. 读取文件

MainAcitvity

技术分享
package com.test.gridview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setAdapter(new ImageAdapter(this));

        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,
                                    int position, long id) {
                Toast.makeText(MainActivity.this, "" + position,
                        Toast.LENGTH_SHORT).show();
            }
        });
    }


}
View Code

ImageAdapter

技术分享
package com.test.gridview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import java.util.List;
import java.util.Map;

/**
 * Created by 1 on 2016/4/17.
 */
public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    public ImageAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return imagePath.size();
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            // if it‘s not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }

        Bitmap bitmap = BitmapFactory.decodeFile(imagePath.get(position).get(FileUtils.IMAGE_PATH));
        imageView.setImageBitmap(bitmap);
        return imageView;
    }



    private List<Map<String, String>> imagePath = FileUtils.getFiles(FileUtils.SDPATH);
}
View Code

FileUtils

技术分享
package com.test.gridview;

import android.os.Environment;

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

/**
 * Created by 1 on 2016/4/17.
 */
public class FileUtils {

    public static String[] imageFormatSet = {"jpg", "png", "gif"};

    public static String SDPATH = Environment.getExternalStorageDirectory() + "/00Meeting/";

    public static String IMAGE_NAME = "imageName";

    public static String IMAGE_PATH = "imagePath";

    /**
     * 判断是否是图片文件
     *
     * @param path
     * @return
     */
    public static boolean isImageFile(String path) {
        for (String format : imageFormatSet) {

            if (path.endsWith(format)) {
                return true;
            }
        }
        return false;
    }

    public static List<Map<String, String>> getFiles(String path) {

        List<Map<String, String>> pathList = new ArrayList<>();

        File file = new File(path);
        File[] files = file.listFiles();
        for (File f : files) {
            if (f.isDirectory()) {
                getFiles(f.getAbsolutePath());
            } else {
                if (isImageFile(f.getPath())) {
                    Map<String, String> map = new HashMap<>();
                    map.put(FileUtils.IMAGE_NAME, f.getName());
                    map.put(FileUtils.IMAGE_PATH, f.getPath());
                    pathList.add(map);

                }
            }
        }
        return pathList;
    }
}
View Code

 

android 显示目录下图片

标签:

原文地址:http://www.cnblogs.com/newlangwen/p/5401184.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!