简体   繁体   English

从SD卡读取网格视图库

[英]Grid view gallery reading from SD card

I have a grid view that loads all the images from a specific folder into itself. 我有一个网格视图,可将特定文件夹中的所有图像加载到自身中。 I'm trying to get it so that when a user click an individual image within the grid view it sets an enlarged version of itself within an image view. 我正在尝试获取它,以便当用户在网格视图中单击单个图像时,它会在图像视图中设置自身的放大版本。 I think what I need to try and do is when the grid view is created and the images loaded into it, I need to attach an array to both the imageview created holding the image within the gridview and the location of it within the SD card. 我认为我需要尝试做的是在创建网格视图并将图像加载到其中时,我需要将一个数组附加到创建的图像视图中,该图像视图将图像保存在网格视图中,并将其放置在SD卡中。 So when an image is loaded; 因此,当加载图像时; save location of image and attach it to the imageview within gridview. 保存图像的位置,并将其附加到gridview中的imageview。 What I have so far loads the last picture loaded from the SD card into the enlarged image view. 到目前为止,我将从SD卡加载的最后一张图片加载到放大的图像视图中。

Main.java Main.java

public class Main extends Activity{

ImageView photoHolder;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gallery);


    photoHolder = (ImageView) findViewById(R.id.photoHolderView);
    photoHolder.setVisibility(View.GONE);
    GridView gridView = (GridView) findViewById(R.id.grid_view);
    gridView.setVisibility(View.VISIBLE);
    gridView.setAdapter(new ImageAdapter(this));
    gridView.setOnItemClickListener(new OnItemClickListener() 
    {
        @Override
        public void onItemClick(AdapterView<?> a, View view, int position, long id) {

            photoHolder.setVisibility(View.VISIBLE);
            photoHolder.setImageURI(ImageAdapter.uri);


    }
});
}

} }

ImageAdapter.java ImageAdapter.java

public class ImageAdapter extends BaseAdapter {

public static Uri uri;

private Context mContext;
File root = new File(Environment.getExternalStorageDirectory()
        + File.separator + "Custom" + File.separator);
private File[] fileName = root.listFiles();
int count = fileName.length;

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

public int getCount() {
    return fileName.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) {

    uri = Uri.fromFile(fileName[position]);

    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.setImageURI(uri);
    return imageView;
}
}

On a side note it crashes quite a lot due to memory leaks, any idea how I would create a "sample/thumbnail" size image whilst loading it from the SD card? 附带一提,由于内存泄漏,它崩溃了很多,你知道如何在从SD卡加载图像时创建“样本/缩略图”大小的图像吗?

You are right in stating that you need to keep track of the filenane/location for each of the thumbnail. 正确地说,您需要跟踪每个缩略图的文件名/位置。 The easiest way would be to amend your getView method and add one line: 最简单的方法是修改getView方法并添加一行:

imageView.setImageURI(uri);
imageView.setTag(uri);   //new line added
return imageView;

Then, in your onItemClickListener you get the clicked imageview - and get its tag - that will be the filename on the SD card: 然后,在onItemClickListener ,获得单击的imageview-并获得其标签-这将是SD卡上的文件名:

public void onItemClick(AdapterView<?> a, View view, int position, long id) {

    photoHolder.setVisibility(View.VISIBLE);
    photoHolder.setImageURI((Uri)view.getTag());
}

As for the memory issue when loading the bitmap, you can use BitmapFactory.Options to indicate the scaling factor - have a look at this page on Android developer website - it contains the sample code you need. 至于加载位图时的内存问题,可以使用BitmapFactory.Options来指示比例因子-在Android开发人员网站上查看此页面 -包含所需的示例代码。

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

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