0

我试图围绕这个问题来解决这个问题。我有一些代码使用CursorLoader从SD卡中查询图像,并在SimpleCursorAdapter的帮助下将它们显示在GridView中。我在网上跟随了一些例子,并通读了文档。我知道我有一些事情是正确的。该代码运行时没有任何错误,但是我无法获取要在网格上显示的缩略图。以下是到目前为止,我已经得到了代码:从SDCard查询图像并将其显示在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。我已经对它进行了一次尝试,但我不知道要为此创建什么样的模型。任何帮助,这是表示赞赏。谢谢。

+0

在这个问题的代码为我工作,我得到它运行为了回答这个问题(尽管使用一个库):http://stackoverflow.com/questions/30407581/get-mediastore-path-of-a - 特定文件夹 –

+0

有趣。我没有考虑过使用库。感谢分享。 – vshl

+0

我没有看到它可以在目录中获取图像的代码。我会给你一个代码示例。 –

回答

0

我不明白使用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对象从查询中返回并且可以遍历,示例代码在上面列出。
  • 另一个有趣的方法是使用类环境的@getExternalStorageDirectory。我用它来在特定目录中查找文件。这种技术实际上可能会更快,因为它在特定目录中搜​​索而不是查询数据库。但是,可能会出现意外的许可问题,特别是SD卡。

试一试,让我们知道发生了什么。

+0

谢谢。我会试试看。有没有办法将获取的图像绑定到RecyclerView ViewHolder? – vshl

+0

@vshl,RecyclerView只是一个GUI组件/类。它与获取图像无关。您仍然从数据库或本机文件系统获取图像。我没有别的办法。 –

0

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

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

它有自定义您的图像查询选项。

相关问题