从SD卡到GridView的图片 [英] Images from SD-card to GridView

查看:126
本文介绍了从SD卡到GridView的图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找关于如何阅读从手机SD卡的图像,然后将它们放置在一个GridView和最终能够单击并选择其中一张图片,并在全尺寸开始显示它的一些信息。

I'm looking for some information about how to read images from the phones SD-card and then place them in a GridView and finally be able to click and select one of the images and display it in full size to begin with.

我在找一个教程或例子,很容易跟踪和了解。我已搜查,但我发现很难找到这样的教程。也许是因为我不知道正确的关键词。我按照在Android开发者网页上的GridView的例子,但是我正在寻找一个延续。

I'm looking for a tutorial or example that is easy to follow and understand. I have searched, but I find it hard to find a tutorial like this. Perhaps it's because I don't know the right key words. I have followed the GridView example on the Android Developer webpage, but I'm looking for a continuation.

到目前为止,我知道我需要MediaStore内容提供商,querys和游标工作。

So far I have learned that I need to work with MediaStore content providers, querys and cursors.

我想preciate,如果有人能够给我一些更多的信息或方向走了。谢谢!

I would preciate if someone could give me some more info or direction to get going. Thanks!

推荐答案

下面是一个简单的例子来添加ImageView的成网格视图。

Here is a simple example to add a imageview into a grid view.

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

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

    gridView.setOnItemClickListener(new OnItemClickListener() 
    {
        public void onItemClick(AdapterView<?> parent, 
        View v, int position, long id) 
        {                
            Toast.makeText(getBaseContext(), 
                    "pic" + (position + 1) + " selected", 
                    Toast.LENGTH_SHORT).show();
        }
    });        
}

public class ImageAdapter extends BaseAdapter 
{
    private Context context;

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

    //---returns the number of images---
    public int getCount() {
        return imageIDs.length;
    }

    //---returns the ID of an item--- 
    public Object getItem(int position) {
        return position;
    }

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

    //---returns an ImageView view---
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        ImageView imageView;
        if (convertView == null) {
            imageView = new ImageView(context);
            imageView.setLayoutParams(new GridView.LayoutParams(185, 185));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(5, 5, 5, 5);
        } else {
            imageView = (ImageView) convertView;
        }
        imageView.setImageResource(...);
        return imageView;
    }
}    

下面是如何把一个图片文件到ImageView的。

Here is how to put a image file into a imageview.

解决方法1:

ImageView i = new ImageView(mContext);  

Bitmap bm = BitmapFactory.decodeFile(...);  
i.setImageBitmap(bm);  

i.setLayoutParams(new Gallery.LayoutParams(150, 100));  
i.setScaleType(ImageView.ScaleType.FIT_XY);  
i.setBackgroundResource(...); 

Solution2:

Solution2:

ImageView im = new ImageView(mContext);   
im.setImageURI(Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, ""+id)); 

这篇关于从SD卡到GridView的图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆