【问题标题】:Query images from SDCard and display them in a GridView从 SDCard 查询图像并在 GridView 中显示它们
【发布时间】:2015-11-03 19:46:53
【问题描述】:

我正在尝试解决这个问题。我有一些代码使用 CursorLoader 从 sdcard 中查询图像,并在 SimpleCursorAdapter 的帮助下将它们显示在 GridView 中。我在网上遵循了一些示例并通读了文档。我知道我做对了部分事情。代码运行没有任何错误,但是我无法在网格上显示缩略图。以下是我目前得到的代码:

GalleryFragment.java

public class GalleryFragment extends Fragment {

    private SimpleCursorAdapter adapter;
    public static final int PHOTO_LIST_ID = 0x01;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_gallery, container, false);
        // Bind adapter to grid
        setupCursorAdapter();
        // Initialize the loader with a special ID and the defined callbacks
        getLoaderManager().initLoader(PHOTO_LIST_ID,
                new Bundle(), imageLoader);
        GridView gridView = (GridView) view.findViewById(R.id.gridView);
        gridView.setAdapter(adapter);
        return view;

    }

    private void setupCursorAdapter() {
        String[] from = {MediaStore.Images.Thumbnails.DATA};
        int[] to = {R.id.imageView};
        adapter = new SimpleCursorAdapter(getActivity().getBaseContext(),
                R.layout.gallery_image_item,
                null, from, to,
                0);
    }

    private LoaderManager.LoaderCallbacks<Cursor> imageLoader =
            new LoaderManager.LoaderCallbacks<Cursor>() {

                @Override
                public Loader<Cursor> onCreateLoader(int id, Bundle args) {
                    // Define the columns to retrieve
                    String[] projection = {MediaStore.Images.Thumbnails._ID,
                        MediaStore.Images.Thumbnails.DATA};
                    return new CursorLoader(getActivity(),
                            MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
                            projection, null, null, null);
                }

                @Override
                public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
                    adapter.swapCursor(data);
                }

                @Override
                public void onLoaderReset(Loader<Cursor> loader) {
                    adapter.swapCursor(null);
                }
            };
}

fragment_gallery.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="edu.sfsu.csc780.pictachio.GalleryFragment">

    <GridView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/gridView"
        android:numColumns="auto_fit"
        android:verticalSpacing="10dp"/>

</LinearLayout>

gallery_image_item.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:padding="10dp"
        android:adjustViewBounds="true"
        android:contentDescription="ImageView" />
</FrameLayout>

我在很多地方读到,推荐使用 RecyclerView 而不是 GridView。我试了一下,但我不知道要为此创建什么样的模型。对此的任何帮助表示赞赏。谢谢。

【问题讨论】:

  • 这个问题中的代码对我有用,我让它运行是为了回答这个问题(虽然使用了一个库):stackoverflow.com/questions/30407581/…
  • 有趣。我没有考虑为此使用库。感谢分享。
  • 我没有看到可以在目录中获取图像的代码。我会给你一个代码示例。

标签: android android-fragments android-gridview android-recyclerview android-cursorloader


【解决方案1】:

我不明白使用 getLoaderManager() 和 initLoader() 来获取图像。一种方法是从 SQLite 数据库中获取它们。正确的 Google 文档是 @SQL query。我提供了一个使用该查询方法的示例代码。但是我从来不需要在 Android 中获取图像。

Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String[] cols = new String[]{
                MediaStore.Images.Media.DATA,  // get the image
};
//String where = MediaStore.Images.Media.DATA;

Cursor cursor = getContentResolver().query(uri, cols, null, null, null);
if(cursor.moveToFirst()) {
   int column_image = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
   res = cursor.getString(column_image);
}
cursor.close();

注意事项:

  • query() 的第三个和第四个参数对于过滤返回的数据很有用。
  • 查询返回cursor对象,可以遍历,示例代码如上。
  • 另一个有趣的方法是使用 Environment 类的@getExternalStorageDirectory。我用它在特定目录中查找文件。这种技术实际上可能更快,因为它在特定目录中搜​​索而不是查询数据库。但是,可能会出现不希望出现的权限问题,尤其是 SD 卡。

试试看,然后告诉我们会发生什么。

【讨论】:

  • 谢谢。我会试试看。有没有办法将获取的图像绑定到 RecyclerView ViewHolder?
  • @vshl,RecyclerView 只是一个 GUI 组件/类。它与获取图像无关。您仍然可以从数据库或本机文件系统中获取图像。我没有其他办法。
【解决方案2】:

最好的方法是使用recent-images 库。就是这么简单。只需将库依赖项添加到您的项目中,然后将下面的行添加到您的类中。您的适配器已准备就绪,您可以为您的 gridview 设置它。

RecentImages ri = new RecentImages();
ImageAdapter adapter = ri.getAdapter(MainActivity.this);

它有一些选项可以自定义您对图像的查询。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-07
    相关资源
    最近更新 更多