简体   繁体   English

从SD卡到GridView的图像

[英]Images from SD-card to 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. 我正在寻找有关如何从手机SD卡读取图像,然后将其放置在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. 我已经在Android开发人员网页上遵循了GridView示例,但是我正在寻找一个延续。

So far I have learned that I need to work with MediaStore content providers, querys and cursors. 到目前为止,我已经了解到我需要使用MediaStore内容提供程序,查询和游标。

I would preciate if someone could give me some more info or direction to get going. 如果有人可以给我更多信息或前进的方向,我将不胜感激。 Thanks! 谢谢!

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

Here is how to put a image file into a imageview. 这是将图像文件放入imageview的方法。

Solution1: 解决方案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: 解决方案2:

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

Here is a beginner and good tutorial on grid view. 是关于网格视图的初学者和优秀教程。 In that tutorial the images are placed in the drawable folder. 在该教程中,图像放置在drawable文件夹中。 since you want to read the images from the memory card, just find the path for those images and supply them to the imageView's in the gridview. 由于您要从存储卡中读取图像,因此只需找到这些图像的路径并将其提供给gridview中的imageView即可。 let me know if you need help with that. 让我知道您是否需要帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM