【问题标题】:Path of a picture in a GridView in AndroidAndroid中GridView中图片的路径
【发布时间】:2016-12-05 16:19:17
【问题描述】:

我在 Android 中使用简单的GridView 来显示照片库中的图片。我的代码可以获取图片路径:

例如:storage/emulated/0/DCIM/Camera/IMG_20161205_101211.jpg

我的GridView 适配器:

public class ImageAdapter extends BaseAdapter {

    private Context mContext;

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

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

    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;
        }

        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }

    // references to our images
    private Integer[] mThumbIds = {
            R.drawable.face, R.drawable.face2,
            R.drawable.face3, R.drawable.face4,
            R.drawable.face5, R.drawable.face6,
            R.drawable.face7, R.drawable.face8
    };
}

我想使用mThumbIds 中的图片路径,而不是使用assets 文件夹中的图片来显示图片,但我真的不知道该怎么做。我应该将图片路径转换为Bitmap吗?

【问题讨论】:

标签: android gridview


【解决方案1】:

使用此代码

try {
    // get input stream put name here
    InputStream ims = getAssets().open("avatar.jpg");
    // load image as Drawable
    Drawable d = Drawable.createFromStream(ims, null);
    // set image to ImageView
    imageView.setImageDrawable(d);
}
catch(IOException ex) {
    return;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-15
    • 2015-10-21
    • 1970-01-01
    • 2014-11-01
    • 2011-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多