在GridView中显示存储在SD卡中的图像

时间:2014-03-27 20:05:59

标签: android gridview

link之后,我设法显示了drawable文件夹中的图像。但是,我想显示位于SD卡中的图像,例如在此文件夹中:

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString() + File.Seperator + "MyApp"

但是我无法修改我的代码以从SD卡中的文件夹中获取图像并将其显示在GridView中。

我的代码显示可绘制文件夹中的图像:

PictureGallery.java

public class PictureGallery extends Activity {

    protected static final String EXTRA_RES_ID = "POS";

    private ArrayList<Integer> mThumbIdsFlowers = new ArrayList<Integer>(
        Arrays.asList(R.drawable.ic_launcher, R.drawable.contrast,
                R.drawable.saltpepper1, R.drawable.saltpepper2,
                R.drawable.ic_launcher, R.drawable.contrast,
                R.drawable.saltpepper1, R.drawable.saltpepper2,
                R.drawable.ic_launcher, R.drawable.contrast,
                R.drawable.saltpepper1, R.drawable.saltpepper2));

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.picture_gallery);

        GridView gridview = (GridView) findViewById(R.id.gridview);

        gridview.setAdapter(new ImageAdapter(this, mThumbIdsFlowers));
    }
}

ImageAdapter.java

public class ImageAdapter extends BaseAdapter {
    private static final int PADDING = 8;
    private static final int WIDTH = 250;
    private static final int HEIGHT = 250;
    private Context mContext;
    private List<Integer> mThumbIds;

    public ImageAdapter(Context c, List<Integer> ids) {
        mContext = c;
        this.mThumbIds = ids;
    }

    @Override
    public int getCount() {
        return mThumbIds.size();
    }

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

    // Will get called to provide the ID that
    // is passed to OnItemClickListener.onItemClick()
    @Override
    public long getItemId(int position) {
        return mThumbIds.get(position);
    }

    // create a new ImageView for each item referenced by the Adapter
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = (ImageView) convertView;

        // if convertView's not recycled, initialize some attributes
        if (imageView == null) {
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(WIDTH, HEIGHT));
            imageView.setPadding(PADDING, PADDING, PADDING, PADDING);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        }

        imageView.setImageResource(mThumbIds.get(position));
        return imageView;
    }
}

我很抱歉问这样一个业余问题。在我的辩护中,我是android和java编程的新手。提前谢谢。

1 个答案:

答案 0 :(得分:2)

PictureGallery.java

public class PictureGallery extends Activity {

    ArrayList<String> images;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.picture_gallery);

        images = new ArrayList<String>();// list of file paths
        getFromSdcard();

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

    File[] listFile;

    public void getFromSdcard()
    {
        File file= new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES), "InsCam + FixBlur");

        if (file.isDirectory())
        {
            listFile = file.listFiles();

            for (int i = 0; i < listFile.length; i++)
            {
                images.add(listFile[i].getAbsolutePath());
            }
        }
    }
}

ImageAdapter.java

public class ImageAdapter extends BaseAdapter {
    private static final int PADDING = 8;
    private static final int WIDTH = 250;
    private static final int HEIGHT = 250;
    private Context mContext;
    private List<String> mThumbIds;

    public ImageAdapter(Context c, List<String> ids){
        mContext = c;
        this.mThumbIds = ids;
    }

    @Override
    public int getCount() {
        return mThumbIds.size();
    }

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

    // Will get called to provide the ID that
    // is passed to OnItemClickListener.onItemClick()
    @Override
    public long getItemId(int position) {
        return position;
    }

    // create a new ImageView for each item referenced by the Adapter
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ImageView imageView = (ImageView) convertView;

        // if convertView's not recycled, initialize some attributes
        if (imageView == null) {
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(WIDTH, HEIGHT));
            imageView.setPadding(PADDING, PADDING, PADDING, PADDING);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        }

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(mThumbIds.get(position), options);

        // Set inSampleSize
        options.inSampleSize = 4;

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        Bitmap myBitmap = BitmapFactory.decodeFile(mThumbIds.get(position), options);

        imageView.setImageBitmap(myBitmap);

        return imageView;
    }
}