且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何创建SD卡上有一个特定文件夹的图片自定义库?

更新时间:2023-09-28 09:15:16

我已经按照/ApiDemos/src/com/example/android/apis/view/Gallery1.java变化

I have made following changes in /ApiDemos/src/com/example/android/apis/view/Gallery1.java

public class ImageAdapter extends BaseAdapter {
    int mGalleryItemBackground;

    public ImageAdapter(Context c) {
        mContext = c;

    }

    public int getCount() {
        return allFiles.length;
    }

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

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

    public View getView(final int position, View convertView,
            ViewGroup parent) {

        ImageView myImageView = new ImageView(mContext);

        if (convertView != null)
            myImageView = (ImageView) convertView;
        else {
            myImageView = new ImageView(mContext);
            myImageView.setLayoutParams(new GridView.LayoutParams(60, 60));
            myImageView.setAdjustViewBounds(false);
            myImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

        }

        Bitmap bitmapImage = BitmapFactory.decodeFile(folder + "/"
                + allFiles[position]);
        BitmapDrawable drawableImage = new BitmapDrawable(bitmapImage);
        myImageView.setImageDrawable(drawableImage);

        return myImageView;

    }

    private Context mContext;

    File folder = new File(
            Environment.getExternalStorageDirectory()
            .getPath()+"/files/Pictures/");
            String[] allFiles = folder.list();


}

因此​​,我们得到这个文件夹中的图片名称的数组。在示例中,我们得到了可绘制的ID数组。

Hence, we get array of image names in this folder. In the sample, we got the array of IDs of the drawables.