使用 gridview 显示 SDCard 上特定文件夹中的图像 [英] Displaying images from a specific folder on the SDCard using a gridview

查看:22
本文介绍了使用 gridview 显示 SDCard 上特定文件夹中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个网格视图,该视图加载了 SDCard 上特定文件夹中的图像.文件夹的路径是已知的, ("/sdcard/pictures") ,但在我在网上看到的示例中,我不确定如何或在哪里指定我想从中加载图像的图片文件夹的路径.我已经阅读了几十个教程,甚至是 developer.android.com 上的 HelloGridView 教程,但这些教程并没有教我我正在寻找什么.

I'm attempting to create a gridview that is loaded with images from a specific folder that resides on an SDCard. The path to the folder is known, ("/sdcard/pictures") , but in the examples I've seen online I am unsure how or where to specify the path to the pictures folder I want to load images from. I have read through dozens of tutorials, even the HelloGridView tutorial at developer.android.com but those tutorials do not teach me what i am seeking.

到目前为止我读过的每个教程都有:

Every tutorial I have read so far has either:

A) 从/res 文件夹中将图像作为 Drawable 调用并将它们放入要加载的数组中,根本不使用 SDCard.

A) called the images as a Drawable from the /res folder and put them into an array to be loaded, not using the SDCard at all.

B) 使用 MediaStore 访问了 SDCard 上的所有图片,但未指定如何设置我要显示图像的文件夹的路径

B) Accessed all pictures on the SDCard using the MediaStore but not specifying how to set the path to the folder I want to display images form

C) 建议使用 BitmapFactory,我对如何使用一无所知.

C) Suggested using BitmapFactory, which I haven't the slightest clue how to use.

如果我以错误的方式处理此问题,请告诉我并指导我采用正确的方法来做我想做的事情.

If I'm going about this in the wrong way, please let me know and direct me toward the proper method to do what I'm trying to do.

推荐答案

好的,经过多次反复尝试,我终于有了一个有效的例子,我想我会分享它.我的示例查询图像 MediaStore,然后获取要在视图中显示的每个图像的缩略图.我正在将我的图像加载到 Gallery 对象中,但这不是此代码工作的要求:

OK, after many iterations of trying, I finally have an example that works and I thought I'd share it. My example queries the images MediaStore, then obtains the thumbnail for each image to display in a view. I am loading my images into a Gallery object, but that is not a requirement for this code to work:

确保您有一个 Cursor 和 int 用于在类级别定义的列索引,以便 Gallery 的 ImageAdapter 可以访问它们:

Make sure you have a Cursor and int for the column index defined at the class level so that the Gallery's ImageAdapter has access to them:

private Cursor cursor;
private int columnIndex;

首先,获取位于文件夹中的图像 ID 的光标:

First, obtain a cursor of image IDs located in the folder:

Gallery g = (Gallery) findViewById(R.id.gallery);
// request only the image ID to be returned
String[] projection = {MediaStore.Images.Media._ID};
// Create the cursor pointing to the SDCard
cursor = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
        projection, 
        MediaStore.Images.Media.DATA + " like ? ",
        new String[] {"%myimagesfolder%"},  
        null);
// Get the column index of the image ID
columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
g.setAdapter(new ImageAdapter(this));

然后,在图库的ImageAdapter中,获取要显示的缩略图:

Then, in the ImageAdapter for the Gallery, obtain the thumbnail to display:

public View getView(int position, View convertView, ViewGroup parent) {
    ImageView i = new ImageView(context);
    // Move cursor to current position
    cursor.moveToPosition(position);
    // Get the current value for the requested column
    int imageID = cursor.getInt(columnIndex);
    // obtain the image URI
    Uri uri = Uri.withAppendedPath( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, Integer.toString(imageID) );
    String url = uri.toString();
    // Set the content of the image based on the image URI
    int originalImageId = Integer.parseInt(url.substring(url.lastIndexOf("/") + 1, url.length()));
    Bitmap b = MediaStore.Images.Thumbnails.getThumbnail(getContentResolver(),
                    originalImageId, MediaStore.Images.Thumbnails.MINI_KIND, null);
    i.setImageBitmap(b);
    i.setLayoutParams(new Gallery.LayoutParams(150, 100));
    i.setScaleType(ImageView.ScaleType.FIT_XY);
    i.setBackgroundResource(mGalleryItemBackground);
    return i;
}

我想这段代码中最重要的部分是 managedQuery,它演示了如何使用 MediaStore 查询来过滤特定文件夹中的图像文件列表.

I guess the most important section of this code is the managedQuery that demonstrates how to use MediaStore queries to filter a list of image files in a specific folder.

这篇关于使用 gridview 显示 SDCard 上特定文件夹中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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